How operator precedence works between % and //?

78 Views Asked by At
5 % 4 // 6 % 3    

o/p is 0, but according to operator precedence it should raise a ZeroDivisionError because it should be interpreted as

5 % 0 % 3 

resulting in an error

Can somebody please elaborate how operator precedence works here?

2

There are 2 best solutions below

0
On BEST ANSWER
5 % 4 // 6 % 3 

just evaluates left to right so

5%4 = 1
1//6 = 0
0%3 = 0

they are all at the same precedence level https://docs.python.org/3/reference/expressions.html#operator-precedence

0
On

It is left to right. If you change your code for :

(5 % 4) // (6 % 3)

it raises a ZeroDivisionError , since parenthesis have precedence. If you don't put parenthesis it's only from left to right.