Booleans
Booleans represent one of two values: True or False.
Syntax
is_ready = TrueA Boolean is either True or False. Note the capital letters — Python is case-sensitive. Booleans are the basis of every decision your program makes.
Truthy and falsy
Any value can be tested as a condition. The values treated as falsy are: False, 0, 0.0, '' (empty string), None, and empty collections like [] or {}. Everything else is truthy.
Example
is_ready = True
print(10 > 5)
print(bool(''))
print(bool('hi'))
print(bool(0))
# Output:
# True
# False
# True
# FalseWhen to use it
- A login system stores an is_authenticated flag that gates access to protected pages.
- A form validator checks multiple boolean conditions before enabling the submit button.
- A game loop uses a running variable set to False to exit cleanly when the player quits.
More examples
Boolean from comparison
A comparison expression produces a bool value stored in a descriptive variable.
age = 20
is_adult = age >= 18
print(is_adult) # True
print(type(is_adult)) # <class 'bool'>Truthy and falsy values
Shows which built-in values are treated as False (falsy) or True (truthy) in conditions.
values = [0, '', None, [], 1, 'hi', [1]]
for v in values:
print(repr(v), '->', bool(v))
# Output:
# 0 -> False
# '' -> False
# None -> False
# [] -> False
# 1 -> True
# 'hi' -> True
# [1] -> TrueBoolean in if statement
Combines two bool variables with logical operators to control program flow.
is_logged_in = True
has_permission = False
if is_logged_in and not has_permission:
print('Access denied: insufficient permissions')
# Output:
# Access denied: insufficient permissions
Discussion