Learn Python comparison chaining with simple examples. Understand how expressions like 0 < amount < balance work, including evaluation steps, short-circuiting, and benefits for beginners.
In Python, the expression:
0 < amount < balance
is an example of comparison chaining.
It allows you to write multiple conditions in a single, clean statement—just like in mathematics (e.g., “amount is between 0 and balance”).
Python internally treats:
0 < amount < balance
as:
(0 < amount) and (amount < balance)
First Check:
Python checks if 0 < amount.
Short-Circuiting:
Second Check:
If the first condition is True, Python checks if amount < balance.
Final Result:
amount) is evaluated only onceUseful when using functions:
0 < get_amount() < balance
| Scenario | Values | Result |
|---|---|---|
| Valid Range | amount = 50, balance = 100 |
True |
| Zero or Negative | amount = 0, balance = 100 |
False |
| Exceeds Balance | amount = 150, balance = 100 |
False |
In languages like C++ or Java, writing:
0 < amount < balance
can give incorrect results.
Why?
0 < amount is evaluated → gives true or falsebalance → which is not intended👉 Python is different: It understands the chain and evaluates it correctly as a combined condition.
You can chain multiple comparisons:
0 < amount < balance < limit
This is equivalent to:
(0 < amount) and (amount < balance) and (balance < limit)
Tutorials, Roadmaps, Bootcamps & Visualization Projects