Python booleans appear in nearly every program you write, sitting inside if-statements, while-loops, and logical expressions that control which code runs and which code is skipped. At first glance, the boolean type seems trivial: it has exactly two values, True and False, and a handful of operators that combine them. But Python's booleans are unusual because they are a subclass of integers, which means True is numerically equal to 1 and False is numerically equal to 0. This design choice has practical consequences that show up in arithmetic, in comparisons, and in the way Python evaluates any object for truthiness.
If you have read the overview of Python data types and the article on the integer data type, you already understand the numeric foundation that booleans inherit. The boolean type exists to give those numeric truths a clear, readable name and to power the conditional logic that every program depends on.
The two boolean values
True and False are Python keywords with a capital first letter, not variables or function calls. They are singletons: there is exactly one True object and one False object in the entire interpreter. Because there is only one of each, you can check for them with the is operator, though in most cases a simple truthiness test is more Pythonic than an explicit comparison to True or False.
The fact that bool is a subclass of int means you can use booleans anywhere an integer is expected. Adding True to a number adds 1. Multiplying by False produces 0. This can be useful for counting the number of True values in a collection by summing them, since sum of True, False, True returns 2. It also means that True equals 1 and False equals 0 both evaluate to True in equality comparisons, which can surprise beginners who expect boolean and integer equality to be separate concerns.
Truthiness: how any value becomes a boolean
Python's most powerful boolean feature is truthiness: the ability to treat any object as if it were True or False in a boolean context. When you write an if-statement, Python does not check whether the value equals True. Instead, it calls the object's internal truth-testing method, which returns a boolean based on the object's contents.
The rules for truthiness are consistent and predictable. The following values are falsy: False itself, the special value None, zero of any numeric type (0, 0.0, and Decimal zero), empty sequences and collections (empty strings, empty lists, empty tuples, empty dictionaries, empty sets, and empty ranges), and objects whose class defines special methods returning False or 0. Everything else is truthy. A non-empty string is truthy. A list with items in it is truthy. A non-zero number is truthy. User-defined class instances are truthy by default.
Here is how truthiness works across several common types:
print(bool(0)) # False
print(bool(42)) # True
print(bool("")) # False
print(bool("hello")) # True
print(bool([])) # False
print(bool([1, 2])) # True
print(bool(None)) # FalseBoolean operators and short-circuit evaluation
Python provides three boolean operators: and, or, and not. Unlike some languages that use symbols like double ampersand and double pipe, Python uses English words for readability. The and operator returns the first falsy operand if there is one, otherwise the last operand. The or operator returns the first truthy operand if there is one, otherwise the last operand. The not operator always returns a boolean, negating the truthiness of its operand.
Both and and or use short-circuit evaluation: the right operand is only evaluated if the left operand does not already determine the result. For and, if the left operand is falsy, the entire expression must be false regardless of the right operand, so Python skips evaluating it. For or, if the left operand is truthy, the entire expression must be true regardless of the right operand, so Python skips it. This enables useful patterns like providing default values where the second part only runs if the first part is empty or None.
Boolean context in control flow
Every if-statement, while-loop, and conditional expression in Python evaluates its condition in a boolean context. You do not need to write an explicit comparison to True when a plain truthiness test means the same thing and is more Pythonic. The explicit comparison is only needed when you specifically want to distinguish True from other truthy values like non-zero numbers or non-empty strings.
The bool built-in function explicitly converts any value to a boolean using the truthiness rules. You rarely need to call it directly because Python evaluates conditions automatically, but it can be useful for storing the result of a truthiness test in a variable for later use or for making a truthiness test explicit for the reader.
The boolean type may be small, but it is the decision-making engine of every Python program. The next article covers a type that is even smaller in one sense, None has only one value instead of two, but it represents a concept, the absence of a value, that is equally fundamental to writing correct Python code.
Rune AI
Key Insights
- bool is a subclass of int; True equals 1 and False equals 0 numerically.
- Use bool() to test the truthiness of any value, or rely on automatic boolean context.
- The and, or, not operators follow short-circuit evaluation rules.
- Empty collections, zero, None, and empty strings are falsy; everything else is truthy.
- Compare booleans with is only when checking for the exact singleton.
- Truthiness is the foundation of all conditional logic and loop control in Python.
Frequently Asked Questions
Are True and False the only boolean values in Python?
What values count as False in Python?
What is short-circuit evaluation in boolean operators?
Conclusion
Python booleans are simple at the surface, just True and False, but their deep integration with integers and the truthiness system makes them powerful. Understanding truthiness, short-circuit evaluation, and the difference between identity and equality for booleans will eliminate an entire class of bugs from your conditional logic.
More in this topic
Create and Assign Variables in Python
Learn every way to create and assign variables in Python, from basic single assignment to expressions, type hints, and practical patterns beginners use every day.
Python Variable Naming Rules
Learn Python's variable naming rules, including allowed characters, reserved keywords, PEP 8 conventions, and how to choose names that make your code readable.
Python Variables Explained
Understand what Python variables are, how they store and reference data, and why Python's variable model is different from other programming languages.