Python comparison operator precedence
Python has a really nice feature - chained comparison, like in math expressions, so
-1 < 0 == False
is actually a syntactic sugar for
-1 < 0 and 0 == False
under the hood.
python operator precedence of in and comparison
The Python manual says in
and ==
are of equal precedence. Thus, they're evaluated from left to right by default, but there's also chaining to consider. The expression you put above ('1' in '11' == True
) is actually being evaluated as...
('1' in '11') and ('11' == True)
which of course is False
. If you don't know what chaining is, it's what allows you to do something like...
if 0 < a < 1:
in Python, and have that mean what you expect ("a is greater than 0 but less than 1").
Python Operators: Math Precedence Comparison operators vs equality operators
Both equality and the greater than and less than operators have the same precedence in Python. But you're seeing something odd because of how an expression with multiple comparison operators in a row gets evaluated. Rather than comparing the results of previous calculations using its rules of precedence, Python chains them together with and
(repeating the middle subexpressions).
The expression 1 > 0 == -1 < 0
is equivalent to (1 > 0) and (0 == -1) and (-1 < 0)
(except that each of the repeated subexpressions, like -1
only gets evaluated once, which might matter if it was a function call with side effects rather than an integer literal). Since the middle subexpression is False
, the whole thing is False
.
In the second version, the parentheses prevent the comparison chaining from happening, so it just evaluates the inequalities independently and then compares True == True
which is True
.
Python order precedence (in vs ==)
The relevant part is comparisons:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z
So you have
key in dict and dict == True
This is very much a unique Python thing.
Comparison operators and 'is' - operator precedence in python?
You're seeing python's operator chaining working
5 > 2 is True
Is equivalent to
5>2 and 2 is True
You can see this in that
>>> 5>2 is 2
Returns True
.
Python is, == operator precedence
What you see here is operator chaining and there is no precedence involved at all!
Python supports expressions like
1 < a < 3
To test that a number is in between 1 and 3; it's equal to (1 < a) and (a < 3)
except that a
is only evaluated once.
Unfortunately that also means that e.g.
None is None == None
actually means
(None is None) and (None == None)
which is of course True, and the longer example you started with
a = b = 3
a is None == b is None
means
(a is None) and (None == b) and (b is None)
which can only be True
if both a
and b
are None
.
Documentation here, see the bit about chaining.
Very useful sometimes but it also pops up when you least expect it!
Comparison Operator Precedence and Binding
As per the documentation, it's not exactly evaluated left to right. The and
's are implicit
It's false because at least one (the first) condition is false, causing a short circuit evaluation
1>=2 and 2==5 and 5<=4
=> False and (doesn't matter)
=> False
What is the precedence of the in operator?
Python chains certain operators, including in
.
This:
True in a in [a]
means
(True in a) and (a in [a])
so if a
is equal to [False,True]
, then the expression is true.
The other versions:
True in a in a
True in a in (a)
are equivalent to each other. Putting parentheses around (a)
doesn't change its type or its value.
Both mean (True in a) and (a in a)
, so unless a
contains itself, they are false.
How does python operator precedence work with double comparisons?
This is a chained comparison. Instead of being left-associative like (-3 < -2) < -1
or right-associative like -3 < (-2 < -1)
, it's actually treated as
(-3 < -2) and (-2 < -1)
except that -2
is evaluated at most once.
Related Topics
Why Is Dictionary Ordering Non-Deterministic
How to Log While Using Multiprocessing in Python
How to Detect Whether a Python Variable Is a Function
Is Python Interpreted, or Compiled, or Both
How to Execute Raw SQL in Flask-Sqlalchemy App
Parse a .Py File, Read the Ast, Modify It, Then Write Back the Modified Source Code
How to Open a File for Both Reading and Writing
Performance of Pandas Apply VS Np.Vectorize to Create New Column from Existing Columns
How to Access the Query String in Flask Routes
How to Validate a Url with a Regular Expression in Python
How to Specify New Lines on Python, When Writing on Files
How to Read a File with a Semi Colon Separator in Pandas
Automatically Create Requirements.Txt
Compare Two Dataframes and Output Their Differences Side-By-Side