Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Practical Pages

Truth value testing in Python

Concept page on Python truth value testing: which values are falsy, how bool() decides, __bool__ and __len__, and common pitfalls with if x and None checks.

Anywhere Python needs a yes/no answer (an if, a while, not, and, or, a comprehension filter, any(), all()) it asks the object for its truth value. You do not need a comparison: if items: is true when the list has something in it. Values that count as true are called truthy, the others falsy.

The rule is simple: an object is truthy unless its class says otherwise. A handful of built-in values are falsy, and your own classes can opt in with __bool__ or __len__.

Python
if name:          # true for any non-empty string
    print("hello", name)

Try it

Python What is falsy?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Your own classes: __bool__ and __len__

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python and / or return operands, not booleans

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The falsy values

The built-in objects that are falsy:

  • None and False
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: "", b"", [], (), {}, set(), frozenset(), range(0)

Everything else is truthy by default, including "0", "False", " ", [0], [[]] and float("nan"). A container is judged by whether it is empty, not by what it contains.

How Python decides

For bool(x) or if x: Python checks, in order:

  1. If the type defines __bool__, call it. It must return a real bool, or TypeError is raised.
  2. Otherwise, if the type defines __len__, the object is falsy when the length is 0.
  3. Otherwise the object is truthy.

Some standard types follow this too: datetime.timedelta(0) is falsy; a datetime.time is always truthy in modern Python (before 3.5 midnight was falsy); NumPy arrays with more than one element raise ValueError when you test them, so use .any() or .all().

Usage notes

  • Prefer if items: over if len(items) > 0: and if not items: over if items == []:. It is shorter, works for every container and matches PEP 8.
  • Check None explicitly with is None. if x: conflates None, 0, "" and [], which is a bug when 0 or an empty string is a valid value.
  • A default argument like def f(limit=None): limit = limit or 10 silently replaces a deliberate 0. Write if limit is None: instead.
  • and/or return one of their operands and short-circuit: the right side is only evaluated when needed. bool(a or b) converts the result when you need a real True/False.
  • Comparison operators return real booleans; == on two lists compares contents, so [] == False is False even though both are falsy.
  • True and False are instances of int (True + True == 2), which is why sum() of booleans counts the true ones.