Python Booleans
Python · Variables & Data Types
Theory
A boolean is one of two values, True or False, of type bool. Comparisons produce them (5 > 3 is True), and if, while and filtering logic consume them. bool is actually a subclass of int, so True behaves like 1 and False like 0: True + True is 2, and sum() of a list of booleans counts the True values, a handy trick in data work.
Every Python object also has a truth value, which is what if checks. Falsy values are False, None, zero of any numeric type (0, 0.0), and empty containers ('', [], (), {}, set()). Everything else is truthy, including negative numbers and non-empty strings. A classic gotcha: bool('False') is True, because it is a non-empty string; converting the text 'False' needs an explicit comparison like value == 'True'.
Why we use it
Booleans are how programs make decisions. Every if, while, filter and validation rule depends on a True/False answer, and knowing truthiness lets you write short checks like if names: instead of if len(names) > 0:.
When to use it
Use booleans for flags (is_active) and results of comparisons. Rely on truthiness for 'is it empty?' checks, but compare explicitly (if x is None, if count == 0) when 0 and None mean different things in your data.
How it works
- Python needs a truth value, e.g. if value:
- Is value None or False? Yes: Treat as False. No: Check further.
- Is it a zero number or an empty container? Yes: Treat as False. No: Treat as True.
- Run the matching branch
- Comparisons like age >= 18 already return True or False
Syntax
x = True
y = 5 > 3Example
is_adult = age >= 18Key points
- Almost any value evaluates truthy except 0, None, and empty collections
- True and False are capitalised; true is a NameError
- bool is a subclass of int: True + True == 2
- bool('False') is True because the string is not empty
- sum(flags) counts how many items are True
- Core to conditional logic