Priority of the logical operators NOT, AND, OR in Python
PythonPython 3.xOperator PrecedenceBoolean ExpressionPython Problem Overview
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?
Python Solutions
Solution 1 - Python
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...}
Solution 2 - Python
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
.
Solution 3 - Python
not
binds tighter than and
which binds tighter than or
as stated in the language reference
Solution 4 - Python
Of the boolean operators the precedence, from weakest to strongest, is as follows:
or
and
not x
is not
;not in
Where operators are of equal precedence evaluation proceeds from left to right.
Solution 5 - Python
Some simple examples; note the operator precedence (not, and, or); parenthesize to assist human-interpretability.
a = 'apple'
b = 'banana'
c = 'carrots'
if c == 'carrots' and a == 'apple' and b == 'BELGIUM':
print('True')
else:
print('False')
# False
Similarly:
if b == 'banana'
True
if c == 'CANADA' and a == 'apple'
False
if c == 'CANADA' or a == 'apple'
True
if c == 'carrots' and a == 'apple' or b == 'BELGIUM'
True
# Note this one, which might surprise you:
if c == 'CANADA' and a == 'apple' or b == 'banana'
True
# ... it is the same as:
if (c == 'CANADA' and a == 'apple') or b == 'banana':
True
if c == 'CANADA' and (a == 'apple' or b == 'banana'):
False
if c == 'CANADA' and a == 'apple' or b == 'BELGIUM'
False
if c == 'CANADA' or a == 'apple' and b == 'banana'
True
if c == 'CANADA' or (a == 'apple' and b == 'banana')
True
if (c == 'carrots' and a == 'apple') or b == 'BELGIUM'
True
if c == 'carrots' and (a == 'apple' or b == 'BELGIUM')
True
if a == 'apple' and b == 'banana' or c == 'CANADA'
True
if (a == 'apple' and b == 'banana') or c == 'CANADA'
True
if a == 'apple' and (b == 'banana' or c == 'CANADA')
True
if a == 'apple' and (b == 'banana' and c == 'CANADA')
False
if a == 'apple' or (b == 'banana' and c == 'CANADA')
True
Solution 6 - Python
There is no good reason for Python to have other priority sequence of those operators than well established one in (almost) all other programming languages, including C/C++.
You may find it in The Python Language Reference, part 6.16 - Operator precedence, downloadable (for the current version and packed with all other standard documentation) from https://docs.python.org/3/download.html, or read it online here: 6.16. Operator precedence.
But there is still something in Python which can mislead you: The result of and
and or
operators may be different from True
or False
- see 6.11 Boolean operations in the same document.
Solution 7 - Python
Expression 1 or 1 and 0 or 0
returns 1
. Looks like we have the same priority, almost same.