When writing conditional logic, a very common task is to check if a value falls within a certain range. For example, is a number between 1 and 10? Is a user’s age between 18 and 65?
Using the tools we already know, we can solve this with the logical and
operator. But Python, in its pursuit of readability and elegance, offers a more intuitive syntax. In this post, we’ll learn about the Pythonic feature of chained comparisons.
The Standard Way: Using the and
Operator
As we learned in our introduction to logical operators (Post #23), we can check for a range by combining two separate comparisons with and
.
Let’s say we want to check if a person’s age falls within the typical working age, which we’ll define as 18 to 64 (inclusive of 18, exclusive of 65).
age = 25
# The standard way to check a range
if age >= 18 and age < 65:
print("This person is of working age.")
This code is perfectly correct, clear, and functional. However, it doesn’t quite look like how we’d write this check in a math textbook.
The Pythonic Way: Chained Comparisons
Python allows us to “chain” comparison operators together to make range checks look exactly like mathematical notation. This makes the code incredibly readable and intuitive.
Let’s rewrite our age check using this beautiful syntax.
age = 25
# The Pythonic way using a chained comparison
if 18 <= age < 65:
print("This person is of working age.")
This line is not only shorter but is also instantly recognizable to anyone with a background in math. It clearly and elegantly expresses the idea of “age is greater than or equal to 18 AND less than 65.”
How it Works
You can think of a chained comparison like a < b < c
as a convenient shortcut for the expression (a < b) and (b < c)
. Python evaluates these comparisons from left to right in a way that is efficient and does what you would naturally expect.
You can chain multiple operators, and you can use any of the standard comparison operators we learned in Post #22 (<
, <=
, >
, >=
, ==
, !=
).
grade = 85
# Check if the grade is a 'B' (from 80 up to, but not including, 90)
if 80 <= grade < 90:
print("The grade is a B.")
temperature = 10
# Check if the temperature is between 0 and 20 degrees (exclusive)
if 0 < temperature < 20:
print("It's cool, but not freezing.")
What’s Next?
Chained comparisons are a perfect example of the Pythonic philosophy in action: “Beautiful is better than ugly,” and “Readability counts.” When you need to check if a value lies within a range, prefer this clean and intuitive syntax over a more verbose and
statement.
We’re continuing our exploration of Python’s elegant syntax shortcuts. Next, we will look at a powerful feature for handling assignments with iterables of unknown length. In Post #118, we will learn how to use the *
operator in assignments, a technique known as “starred expressions.”
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com