Operator precedence in Python -PEMDAS

时间秒杀一切 提交于 2019-12-04 05:03:24

问题


I read about python following PEMDAS that is precedence of multiply is more than division.

I ran the following script

print 6*2/1*2

Thus python should interpret this like 12/2 i.e 6 , since precedence of multiplication is more than division.

But, the answer is 24. Could anyone let me know where the problem is? Thanks!


回答1:


* 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



回答2:


Order of precedence in Python

P

E

M D Left to right

A S Left to right



来源:https://stackoverflow.com/questions/35845566/operator-precedence-in-python-pemdas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!