Basics
Truthiness
An empty list is false, so not items reads as "items is empty". The condition tests the object directly — no len(items) == 0 comparison is needed.
Source
items = []
name = "Ada"
if not items:
print("no items")Output
no itemsA non-empty string is true, so if name: asks "did we get a name?" in one word. Reach for an explicit comparison instead when the distinction matters — if name is not None: treats an empty string differently from a missing one.
Source
if name:
print("has a name")Output
has a namebool() reveals the truth value any condition would use. Zero-like numbers convert to False; other numbers convert to True.
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.