Python Booleans
In this page:
What Is a Boolean
The bool type has exactly two values, True and False, and Python treats them as a special case of integers under the hood — True equals 1 and False equals 0 in numeric contexts. Booleans are what every comparison operator (like == or >) actually returns, which is why they show up constantly in conditions.
Example: What Is a Boolean
print(True == 1)
print(False == 0)
Comparison Results
Every relational operator — ==, !=, <, >, <=, >= — evaluates to a boolean, which is why you can store the result of a comparison in a variable and reuse it later instead of repeating the comparison. This is the mechanism every if statement relies on internally.
Example: Comparison Results
is_adult = 20 >= 18
print(is_adult)
Truthy and Falsy Values
Only a handful of values count as falsy in Python: 0, 0.0, None, and any empty collection like '', [], or {}. Every other value — including negative numbers and single-space strings — is truthy, which is why 'if my_list:' is idiomatic for checking whether a list has any elements.
Example: Truthy and Falsy Values
print(bool(0), bool(""), bool([]), bool(None))
print(bool(-1), bool(" "))
The bool() Function
Calling bool() directly on any value applies Python's truthiness rules and returns an explicit True or False, which is useful for debugging or for storing a condition's truthiness as an actual boolean rather than relying on an if statement to interpret it implicitly.
Example: The bool() Function
value = "some text"
print(bool(value))
Boolean Logic Operators
The and, or, and not keywords combine or invert booleans — and returns the first falsy operand or the last value if all are truthy, or returns the first truthy operand, and this short-circuiting behavior means an expression like x and x.value is safe even if x could be None.
Example: Boolean Logic Operators
x = None
print(x and x.upper())
print(True or False)
print(not False)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: