Basics

Truthiness

Python conditions use truthiness, not only explicit booleans.

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 items
bool(x) is True except for a small fixed set: 0, 0.0, "", [], {}, None, False.

A 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 name

bool() 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
True

Notes

See also

Run the complete example

Example code

Expected output

no items
has a name
False
True

Execution time appears here after you run the example.