Last modified: Oct 28, 2026
Python Chained Comparisons Explained
In Python, comparing values is a fundamental operation. You often need to check if a number falls between two limits or if multiple conditions are true. Chained comparisons make this process simple and readable.
Instead of writing long expressions with multiple and operators, Python lets you chain comparisons naturally. This feature improves code clarity and reduces errors.
What Are Chained Comparisons?
Chained comparisons allow you to evaluate multiple relational operations in a single expression. For example, checking if a value is between 10 and 20 becomes very intuitive.
# Traditional approach
x = 15
if x > 10 and x < 20:
print("x is between 10 and 20")
# Chained comparison approach
if 10 < x < 20:
print("x is between 10 and 20")
x is between 10 and 20
x is between 10 and 20
Both approaches produce the same result. However, the chained version reads more like natural language.
How Chained Comparisons Work Internally
Python evaluates chained comparisons from left to right. Each comparison is evaluated independently. Then, Python combines the results using the and operator implicitly.
For example, the expression 10 < x < 20 is equivalent to (10 < x) and (x < 20).
x = 15
result = 10 < x < 20
print(result) # Output: True
# Equivalent expression
result_equivalent = (10 < x) and (x < 20)
print(result_equivalent) # Output: True
True
True
This internal mechanism ensures consistency and prevents redundant evaluations.
Common Use Cases
Range Checking
One of the most common uses is checking if a value lies within a specific range.
temperature = 25
# Check if temperature is comfortable
if 20 <= temperature <= 30:
print("Temperature is comfortable")
Temperature is comfortable
Grade Evaluation
You can use chained comparisons to assign grades based on score ranges.
score = 85
if score >= 90:
grade = "A"
elif 80 <= score < 90:
grade = "B"
elif 70 <= score < 80:
grade = "C"
elif 60 <= score < 70:
grade = "D"
else:
grade = "F"
print(f"Grade: {grade}")
Grade: B
Chaining Different Operators
Python allows you to mix different comparison operators in a single chain.
a = 5
b = 10
c = 15
# Chain multiple different operators
result = a < b == c - 5
print(result) # Output: True
# Another example
x = 3
y = 6
z = 9
result2 = x < y < z
print(result2) # Output: True
True
True
This flexibility makes Python chained comparisons very powerful for complex conditions.
Important Considerations
While chained comparisons are powerful, they can sometimes lead to confusion if not used carefully. Here are some key points to remember:
Operator Precedence
Understanding operator precedence is crucial when working with chained comparisons. Refer to our detailed guide on Python Operator Precedence: Rules and Examples to master how different operators interact.
Short-Circuit Evaluation
Python uses short-circuit evaluation in chained comparisons. If the first comparison is false, subsequent comparisons are not evaluated.
# Example with side effects
x = 5
def check_value(n):
print(f"Checking {n}")
return n < 10
# Only first check runs due to short-circuit
result = check_value(5) and check_value(15)
print(result)
Checking 5
False
Comparison with Bitwise Operators
When working with binary data, you might need bitwise operations. Our guide on Python Bitwise Operators Explained covers these operators in detail.
Using Assertions with Chained Comparisons
You can combine chained comparisons with assertions for better validation. Learn more about assertions in Python Assert Statement Explained.
def validate_age(age):
assert 0 < age < 120, "Age must be between 0 and 120"
return f"Valid age: {age}"
print(validate_age(25))
Valid age: 25
Best Practices
Follow these best practices for cleaner code:
- Use chained comparisons for range checks instead of multiple
andoperators - Keep chains readable and avoid overly complex expressions
- Consider using parentheses for clarity when mixing different operators
- Test edge cases to ensure correct behavior
Advanced Examples
Here are some advanced patterns using chained comparisons:
# Multiple range checks
values = [1, 5, 10, 15, 20]
# Filter values between 5 and 15
filtered = [v for v in values if 5 <= v <= 15]
print(filtered)
# Complex condition with mixed operators
a, b, c = 10, 20, 30
condition = 0 < a < b < c < 100
print(condition)
[5, 10, 15]
True
Conclusion
Python chained comparisons provide a clean and efficient way to evaluate multiple conditions. They improve code readability and reduce the need for verbose logical expressions. By understanding how they work internally and following best practices, you can write more Pythonic code.
Remember to consider operator precedence, short-circuit evaluation, and readability when using chained comparisons. With practice, this feature will become a valuable tool in your Python programming toolkit.