Priority of the logical statements NOT AND & OR in python

Akshar Gupta picture Akshar Gupta · May 21, 2013 · Viewed 82.6k times · Source

As far as I know, in C & C++, the priority sequence for NOT AND & OR is NOT>AND>OR. But this doesn't seem to work in a similar way in Python. I tried searching for it in the Python documentation and failed (Guess I'm a little impatient.). Can someone clear this up for me?

Answer

Kyle Heuton picture Kyle Heuton · May 21, 2013

It's NOT, AND, OR, from highest to lowest according to the documentation on Operator 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. if – else
 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...}