Home >Backend Development >Python Tutorial >What is the order of precedence for NOT, AND, and OR operators in Python?
Priority (precedence) of the logical operators (order of operations) for NOT, AND, OR in Python
Unlike C and C, Python's logical operators have a distinct precedence sequence: NOT, AND, OR. Understanding this order is crucial when evaluating complex logical expressions.
NOT precedes AND and OR
According to Python's documentation on operator precedence, the order of precedence is as follows:
This means that NOT has the highest precedence, followed by AND, then OR.
A Precedence Table
For clarity, here is a complete precedence table from lowest to highest precedence:
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...}
By understanding this operator precedence, you can effectively evaluate logical expressions and ensure correct code execution.
The above is the detailed content of What is the order of precedence for NOT, AND, and OR operators in Python?. For more information, please follow other related articles on the PHP Chinese website!