Operator precedence in Python -PEMDAS

2019-03-01 05:19发布

问题:

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