Is there any computational difference between these two methods of checking equality between three objects?
I have two variables: x
and y
. Say I do this:
>>> x = 5
>>> y = 5
>>> x == y == 5
True
Is that different from:
>>> x = 5
>>> y = 5
>>> x == y and x == 5
True
What about if they are False
?
>>> x = 5
>>> y = 5
>>> x == y == 4
False
And:
>>> x = 5
>>> y = 5
>>> x == y and x == 4
False
Is there any difference in how they are calculated?
In addition, how does x == y == z
work?
Thanks in advance!
Python has chained comparisons, so these two forms are equivalent:
except that in the first, y is only evaluated once.
This means you can also write:
etc. You can also write confusing things like:
Beginners sometimes get tripped up on this:
which will always be false since it is the same as: