Python Booleans
Satellite · Data Types
Python Booleans
Booleans drive control flow. Learn when values evaluate to True/False and how to avoid subtle bugs.
bool is a subclass of int
True and False are the only bool values, and they double as the integers 1 and 0:
True + True # 2
sum([True, False, True]) # 2 — handy for counting matches
isinstance(True, int) # True
Truthiness
Any object can be tested in a boolean context. These values are falsy:
False,None- Zero of any numeric type:
0,0.0,0j - Empty sequences and collections:
"",[],(),{},set(),range(0)
Everything else is truthy. This is why you write if items: instead of if len(items) > 0:.
bool("") # False
bool("0") # True — a non-empty string is truthy, even "0"
bool([]) # False
Logical operators
| Operator | Behavior |
|---|---|
and | Returns the first falsy operand, else the last operand |
or | Returns the first truthy operand, else the last operand |
not | Returns a real bool, negating truthiness |
Crucially, and/or return one of the operands, not necessarily True/False. They also short-circuit—the right side isn't evaluated if the left already decides the result:
name = user_name or "guest" # default if user_name is falsy
user = cached_user or fetch_user() # fetch only runs if cache misses
config and config.get("debug") # avoids AttributeError when config is None
Aggregating with any and all
any([False, True, False]) # True — at least one truthy
all([1, 2, 3]) # True — every element truthy
all([]) # True — vacuously true for empty iterables
any(x > 100 for x in data) # works with generators, short-circuits
Common pitfalls
if x == True: # avoid — fails for truthy non-bool values like 2
if x is True: # avoid — too strict, only matches the literal True
if x: # prefer — plain truthiness test
Reserve is True / is False for when you truly must distinguish the boolean singletons from other truthy values.
Next up in your learning path
Frequently Asked Questions
What values are falsy in Python?
False, None, numeric zero (0, 0.0, 0j), and empty containers ("", [], (), {}, set(), range(0)). Everything else is truthy, including the string "0" and the list [0].
bool(0) # False
bool("") # False
bool([]) # False
bool("0") # True — non-empty string
bool([0]) # True — non-empty list
Why does "a" and "b" return "b" instead of True?
and and or return one of their operands, not a boolean. and returns the first falsy value or the last operand; or returns the first truthy value. Wrap the expression in bool() if you specifically need True or False.
"a" and "b" # 'b'
"" or "x" # 'x'
0 and 5 # 0
bool("a" and "b") # True
Should I write if x == True or if x?
Use if x. It reads naturally and works for any truthy value. if x == True fails for truthy non-booleans, and if x is True only matches the exact True singleton.
x = 2
if x: # True — the idiomatic check
...
x == True # False! (2 != True)
x is True # False (not the True object)
Is True the same as 1 in Python?
Effectively yes. bool is a subclass of int, so True equals 1 and False equals 0. That lets you sum booleans to count how many conditions are true, but avoid relying on it where it hurts readability.
True == 1 # True
sum([True, False, True]) # 2 — counts the True values