Basics
Truthiness
Truthiness is one of Python's most important conveniences: conditions can test objects directly instead of requiring explicit boolean comparisons everywhere.
Empty containers, numeric zero, None, and False are false; most other values are true. This makes common checks such as if items: concise and idiomatic.
Source
items = []
name = "Ada"
if not items:
print("no items")Output
no itemsUse truthiness when it reads naturally, but choose explicit comparisons when the distinction matters, such as checking whether a value is exactly None.
Source
if name:
print("has a name")Output
has a nameUse truthiness when it reads naturally, but choose explicit comparisons when the distinction matters, such as checking whether a value is exactly None.
Source
print(bool(0))
print(bool(42))Output
False
TrueNotes
- Empty containers and zero-like numbers are false in conditions.
- Use explicit comparisons when they communicate intent better than truthiness.
See also
- related: Booleans
- related: None
- next depth: Conditionals
- next depth: Special Methods
Run the complete example
Expected output
no items
has a name
False
True
Execution time appears here after you run the example.