**New course!**Every coder should learn

**Generative AI!**

0

# precedence

Hi everyone, I really don't get this question from a python challenge: print(int(2/2*2-2+2%2)) The correct output is 0 But why? The precedence of multiplication is higher than division, so I supposed it must be calculate in this way: (2/(2*2))-(2+(2%2)) So, where is the error in my logic?

5 Answers

+ 1

2/2 = 1
1*2 = 2
2 - 2 = 0
0 + (2%2) = 0 because: 2%2 = 0
The precedence of multiplication is not higher the division.
You do them in the order they come. The correct way to add parentheses is:
((2/2)*2)-(2)+(2%2)

+ 1

It is always relevent but the acronime PEMDAS helps you remember the rule, it is not the rule itself. It would probibly be better stated PE(M&D)(A&S)
You do parenthese, exponets, multiplication and division (starting on the left), Addision and subtraction (also starting on the left)

0

Ok, thanks. So the PEMDAS-rule (https://techvidvan.com/tutorials/JUMP_LINK__&&__python__&&__JUMP_LINK-operator-precedence/) isn't working here? Why?

0

No, if you read all of it, it clearifies
“The associative operators are division, multiplication, remainder, etc. and the expressions will be evaluated from left to right.”

0

But this doesn't explain to me why the order of PEMDAS is irrevelant. Or to ask from another direction: in which cases does the PEMDAS-rule has a meaning?