Python Mastery – From Basics to Brilliance / Operators in Python

Chained Comparisons in Python

Python allows you to chain multiple comparisons in a single line:

x = 7
if 5 < x < 10:
    print("x is between 5 and 10")

This is equivalent to:

if 5 < x and x < 10:

🔍 Why Use It?

  • Cleaner syntax

  • Easier to read and understand

💡 More Examples:

x = 20
print(10 < x <= 20)  # True
print(5 == x < 30)   # False

✅ Python evaluates chained comparisons in sequence, and reuses the middle value for better performance.


🧠 Example:

Imagine checking if someone’s age is between 18 and 60:

if 18 <= age <= 60:
    print("Eligible")

You don’t need two separate conditions — chaining makes it more natural.