Truthiness & Boolean Logic
beginner10 min readLesson 14 of 169
Falsy values, and/or/not, and chained comparisons.
Python tests truthiness: every value is True or False in a condition.
The falsy gang: 0, 0.0, "", [], {}, None, False. Everything else
is truthy.
name = ""
if not name:
print("name is empty โ why not falsy check instead of len?")
Truthiness makes conditions read like English โ but be explicit when 0 is a meaningful value rather than "missing".
Boolean logic
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("enter")
| Expression | True when |
| --- | --- |
| a and b | both truthy |
| a or b | at least one truthy |
| not a | a is falsy |
Chained comparisons
A Python treat: 18 <= age < 65 does what it says. Use it โ it matches how
humans write ranges.