Chained Comparisons in Python
Janne Kemppainen |Can you chain comparison operations in Python? Yes you can, each comparison is evaluated pairwise so you can chain together as many of them you want.
It can be easy to forget to use basic features like this if you come from a language that doesn’t support chained comparisons or if you’ve never seen them used in the wild. I’ve been using Python professionally for years but I have to admit that I still didn’t really know about this feature until recently.
The classic case is to say that a value needs to be within a certain range. This can be expressed intuitively in Python:
if 1 <= x < 10:
...
It is equivalent to:
if 1 <= x and x < 10:
...
Chained comparisons act as if single comparisons were joined together with AND.
This causes some interesting properties. Take a look at this example.
if a < b < c < d:
...
Only the items around the operators are compared against each other so in theory this statement doesn’t say anything about the relationship between a
and c
, for example. However, with numerical values a < c
also holds true if the whole comparison evaluates to true (although your custom class could have some strange rock-paper-scissors comparison).
Now let’s consider another example.
if a < b > c < d:
...
Note how the comparison between b
and c
was flipped. I don’t think this would fly in your mathematical essay but it is completely valid Python. Remember that the comparisons happen pairwise.
In this example we check that a < b
but we can’t tell if a < d
is also true. Let’s list the pairwise comparisons to make this clearer:
a
is smaller thanb
b
is greater thanc
c
is smaller thand
Therefore b
is greater than both a
and c
, and d
is guaranteed to be greater than c
but it can still be smaller than both a
and b
.
If you didn’t understand a thing of what I’m trying to say here then you’ll probably agree with me that you shouldn’t use something like this in real code.
Bonus tip: If you need to check that the value is an integer between, for example one and ten, you can use the range()
function:
if x in range(1, 11):
...
With range()
you just have to remember that the first value is included but the last one is not so you need to increment that by one. And because the Python 3 range implementation is lazy and doesn’t return a list you can safely use it with large numbers too:
>>> 1234112 in range(1234, 98809810234981324)
True
Read next in the Python bites series.
Previous post
Create a Github Action with JavaScriptNext post
Dynamic Attributes in Python