Booleans
True and False, truthiness, comparison, and the and / or / not operators
bool has exactly two values: True and False. Both are written capitalized, and both are singleton objects. Understanding how Python evaluates truthiness—what counts as true or false in a boolean context—is key to writing idiomatic code.
Every snippet on this page runs in your browser—no setup required.
Real-world context: Truthiness everywhere
Booleans and boolean logic are fundamental to control flow. In real-world code, you'll see:
- Conditional checks:
if user.is_authenticated:,if response.ok:,if items: - Filtering:
[x for x in data if x > 0] - Short-circuit evaluation:
value = user_input or default - Guard clauses:
if not ready: return
Python's truthiness rules make code concise and readable. Instead of if len(items) > 0:, you write if items:.
The two boolean values
The origin of 'boolean'
The term "boolean" comes from George Boole (1815–1864), an English mathematician who developed Boolean algebra—a system of logic with just two values: true and false. His work laid the foundation for digital circuit design and all of computer science.
bool is a subclass of int
bool inherits from int, so True and False behave as 1 and 0 in arithmetic contexts.
The 'is True' antipattern
Never write if x is True:—it checks whether x is the exact True object, not whether x is truthy. This breaks with values like 1, "hello", or [1, 2, 3] that are truthy but not True.
Bad:
if len(items) > 0 is True: # convoluted and wrongGood:
if items: # Pythonic truthiness checkComparison operators
| Operator | Meaning |
|---|---|
==, != | value equality / inequality |
<, <=, >, >= | ordering |
is, is not | identity (same object?) |
in, not in | membership in a sequence/container |
== vs is
== asks "are these equal in value?". is asks "are these the same object in memory?".
For singletons like None, True, False, always use is.
Truthiness
Any object can be tested in a boolean context (e.g., if, while, and, or). Python's rules:
- Falsy:
False,None, any zero (0,0.0,0j), any empty container ("",[],(),{},set()), and instances of classes that define__bool__to returnFalseor__len__to return0. - Truthy: everything else.
This is why idiomatic Python writes if items: instead of if len(items) > 0:.
The 0 == False surprise
Because False equals 0, you can get unexpected behavior if you're not careful:
count = 0
if count == False: # True! (but don't write this)
print("count is zero")Better: use truthiness or an explicit comparison:
if not count: # Pythonic
if count == 0: # explicitLogical operators: and, or, not
Python's logical operators are written as English words, not symbols (&&, ||, ! like in C or JavaScript).
Pure boolean logic
Short-circuit evaluation
and and or short-circuit: they stop evaluating as soon as the result is determined.
Operators return operands, not just True/False
Key insight: and and or don't always return True or False. They return one of the operands.
x and yreturnsxifxis falsy, otherwisey.x or yreturnsxifxis truthy, otherwisey.
Why operators return operands
This design enables a common idiom: using or to supply a default value.
name = user_input or "Anonymous"If user_input is an empty string (falsy), name becomes "Anonymous". If it's a non-empty string (truthy), name becomes user_input.
Gotcha: This breaks if 0 is a valid input, because 0 is falsy:
count = user_count or 10 # if user_count is 0, this gives 10!For such cases, use an explicit None check:
count = user_count if user_count is not None else 10Common idiom: default values
Conditional expressions (ternary operator)
Python's ternary form puts the condition in the middle:
Challenges
Define a function xor(a, b) that returns True if exactly one of a or b is truthy (not both, not neither). Do not use the ^ operator—use and, or, and not.
Write a function all_truthy(items) that returns True if all items in the list are truthy, and False otherwise. Do not use the built-in all()—implement it yourself with a loop or comprehension.
Check your understanding
Which of these expressions is truthy?
0
{}
None
[0]
What does None or 0 or "" or "x" evaluate to?
None
0
""
"x"
What does [] and "hello" evaluate to?
True
False
[]
"hello"
What is the idiomatic way to check if a list is non-empty?
if len(items) > 0:
if items:
if items is not None:
if items == True:
Booleans tie directly into control flow. Before we get there, let's look at Python's main sequence type: the list.