Truthiness, Boolean logic, and chaining comparisons
In Python, any object can be evaluated in a Boolean context (such as an if condition) to determine truthiness – whether it’s considered “true” or “false” when checked in a conditional. We also use boolean operators like and, or, and not to build more complex conditions, and Python offers a unique feature of chaining comparison operators for succinct multiple comparisons.
Truthiness in Python
Truthiness refers to how Python treats values when a Boolean is required. You may use non-Boolean values in an if or other logical expression, and Python will decide if they are “truthy” (treated as True) or “falsy” (treated as False). The basic rule is: “empty” or “zero” values are falsy, and everything else is truthy. Some common falsy values in Python include:
-
FalseandNone -
Zero of any numeric type:
0,0.0,0j(complex zero) -
Empty sequences or collections:
""(empty string),[](empty list),()(empty tuple),{}/set()(empty dict or set) -
Objects of custom classes that define a
__bool__method returningFalseor__len__returning 0
Everything that isn’t in the above category is considered truthy (e.g. non-zero numbers, non-empty strings/collections, objects without a falsy override).
Example:
values = []
if values:
print("List has some items")
else:
print("List is empty (falsy)")
Here, the list values is empty, so if values: evaluates to False and the else branch executes, printing that the list is empty. If we append any item to values, the same check would be truthy and the first message would print.
Python internally calls bool(value) to determine truthiness. You can use this function yourself to test:
bool(42) # True
bool(0) # False
bool("Hello") # True
bool("") # False
Caution: While truthiness is convenient, be explicit when clarity matters. For example, if value == 0: is clearer than if not value: when you specifically mean to detect zero.
Boolean Logic Operators (and, or, not)
-
and→ True if both conditions are True -
or→ True if at least one condition is True -
not→ Reverses the truth value
Example:
is_member = True
age = 20
if is_member and age >= 18:
print("Eligible for membership perks.")
if age < 13 or age > 65:
print("Eligible for a discounted ticket.")
if not is_member:
print("Please sign up for membership to get perks.")
Short-circuiting:
-
In
cond1 and cond2, ifcond1is False,cond2is never evaluated. -
In
cond1 or cond2, ifcond1is True,cond2is never evaluated.
This avoids errors — for example:
if user and user.is_admin:
...
will not try to access user.is_admin if user is None.
Chaining Comparison Operators
Python allows chaining comparisons to make them more concise:
if a < b < c:
...
This is equivalent to:
if a < b and b < c:
...
Chaining works for ranges:
if 18 <= age < 65:
print("Working-age adult")
And also for equality:
if x == y == z:
...
Under the hood: x < y <= z is evaluated as (x < y) and (y <= z), with y only evaluated once.
Mini-Exercises:
-
Truthiness test:
items = [0, 1, [], [5], "", "Hi"]
for item in items:
if item:
print("Truth!")
else:
print("Falsy!")
-
Budget check:
price = 250
if 100 <= price <= 500:
print("Within budget")
else:
print("Out of budget")
-
Short-circuit thought:
Why use:
if user and user.is_admin:
instead of:
if user.is_admin:
Because if user is NoneThe second form raises an error, but with and The second condition is never checked.
