Search code examples
pythonpython-3.xoperatorsmodulusfloor-division

How operator precedence works between % and //?


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?


Solution

  • 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