Basics

Truthiness

Python conditions use truthiness, not only explicit booleans.

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

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

Use 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
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.