Python -PEMDAS中的运算符优先级 [英] Operator precedence in Python -PEMDAS
本文介绍了Python -PEMDAS中的运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在PEMDAS之后阅读了有关python的python的知识,即乘除比除法更重要.
I read about python following PEMDAS that is precedence of multiply is more than division.
我运行了以下脚本
print 6*2/1*2
因此python应该将其解释为12/2即6,因为乘法的优先级大于除法.
Thus python should interpret this like 12/2 i.e 6 , since precedence of multiplication is more than division.
但是,答案是24. 谁能让我知道问题出在哪里?谢谢!
But, the answer is 24. Could anyone let me know where the problem is? Thanks!
推荐答案
*
具有相同的运算符优先级作为/
.同一组中的运算符从左到右求值,因此您的表达式的求值为:
*
has the same operator precedence as /
. Operators in the same group evaluate left to right, so your expression evaluates as:
6*2 = 12
/ 1 = 12
* 2 = 24
这篇关于Python -PEMDAS中的运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文