Priority of the logical statements NOT AND & OR in python Priority of the logical statements NOT AND & OR in python python python

Priority of the logical statements NOT AND & OR in python


It's NOT, AND, OR, from highest to lowest according to the documentation onOperator precedence

Here is the complete precedence table, lowest precedence to highest. A row has the same precedence and chains from left to right

 0. := 1. lambda 2. ifelse 3. or 4. and 5. not x 6. in, not in, is, is not, <, <=, >, >=, !=, == 7. | 8. ^ 9. & 10. <<, >> 11. +, - 12. *, @, /, //, % 13. +x, -x, ~x 14. ** 14. await x 15. x[index], x[index:index], x(arguments...), x.attribute 16. (expressions...), [expressions...], {key: value...}, {expressions...}


You can do the following test to figure out the precedence of and and or.

First, try 0 and 0 or 1 in python console

If or binds first, then we would expect 0 as output.

In my console, 1 is the output. It means and either binds first or equal to or (maybe expressions are evaluated from left to right).

Then try 1 or 0 and 0.

If or and and bind equally with the built-in left to right evaluation order, then we should get 0 as output.

In my console, 1 is the output. Then we can conclude that and has higher priority than or.


not binds tighter than and which binds tighter than or as stated in the language reference