Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Truthy and Falsy Values Explained

Learn what truthy and falsy mean in Python, see which standard values are falsy in Boolean contexts, and how to use truthiness in conditionals and custom classes.

~3 min readPython
#Python#truthy#falsy#boolean context
Diagram showing Python truthy and falsy values with examples of objects evaluated in boolean context.

The Boolean Context in Python

In Python, every object can be evaluated in a Boolean context, meaning it is treated as either True or False when used in an if statement, while loop, or with Boolean operators like and, or, and not. This behavior is called truthiness: a value is truthy if bool(value) returns True, and falsy if bool(value) returns False.

python
print(bool(0)) # False print(bool(1)) # True print(bool([])) # False print(bool([1, 2])) # True

Most objects are True by default. Knowing the specific values that are False helps you write concise and idiomatic conditionals.

Standard Falsy Values

Python's standard falsy values include:

ValueExample
Nonebool(None) is False
Falsebool(False) is False
Zero numeric values0, 0.0, 0j, Decimal(0), Fraction(0, 1)
Empty sequences'', b'', (), []
Empty mappings{}
Empty setsset()
Custom objects with __bool__ returning FalseCustom classes can define this
Custom objects with __len__ returning 0If __bool__ is not defined, Python falls back to __len__

Decimal(0) and Fraction(0, 1) are falsy when imported from the decimal and fractions modules. The table covers the commonly encountered falsy built-in values and the two custom object behaviors. Other built-in objects are True by default, such as non-empty strings, lists, tuples, dictionaries, sets, and any non-zero number.

python
falsy_values = [None, False, 0, 0.0, '', [], (), {}, set()] for value in falsy_values: print(f'{value!r} -> {bool(value)}')

How Python Decides Truthiness for Custom Objects

For user-defined classes, Python checks for a __bool__ method first. If it exists, the method's return value is used directly. If __bool__ is not defined, Python looks for __len__. If __len__ is defined and returns 0, the object is False; otherwise it is True. If neither method is defined, the object is always True.

python
class Account: def __init__(self, balance): self.balance = balance def __bool__(self): return self.balance > 0 account = Account(0) print(bool(account)) # False account.balance = 100 print(bool(account)) # True

Implementing __bool__ gives you precise control over how instances behave in conditionals. This is useful for domain objects where emptiness or validity is not simply about being None.

Using Truthiness in Conditionals and Loops

The most common use of truthiness is in if statements. Instead of comparing to an empty list or zero explicitly, you can rely on the object's truth value:

python
items = [] if items: print('Items exist') else: print('No items')

This is more readable than if len(items) > 0. The same applies to while loops and Boolean expressions:

python
while queue: # process until queue is empty item = queue.pop() process(item)

Using truthiness also works with and and or. For example, a or b returns a if a is truthy, otherwise b. This is a common pattern for providing default values:

python
name = user_input or 'default'

Here, if user_input is an empty string (falsy), the default is used. This is concise and idiomatic, but be careful: it also treats 0, None, and empty containers as falsy, which may not always be the intent.

Common Pitfalls and How to Avoid Them

A frequent mistake is treating all falsy values as None. For example, if a function returns 0 or an empty list as a valid result, using if result: will treat it as missing. In such cases, you should check for None explicitly:

python
def find_user(id): # returns None if not found, otherwise a User object ... user = find_user(42) if user is not None: # process user

Another pitfall is relying on truthiness for numeric values. If 0 is a meaningful input, if value: will skip it. Instead, use if value is not None: or compare to 0 directly.

When working with pandas or NumPy, using an array in a Boolean context can raise an error because arrays have ambiguous truth values. This is a separate topic, but it is worth remembering that the standard Python rules do not apply to every library.

When to Override Truthiness in Your Classes

You should override __bool__ when an object's Boolean meaning is not obvious from its default state. For example, a Transaction might be considered True only if it has been approved. A Configuration object might be False if it is missing required settings.

python
class Configuration: def __init__(self, settings): self.settings = settings def __bool__(self): return bool(self.settings.get('enabled', False)) config = Configuration({}) print(bool(config)) # False

Overriding __len__ is useful for collection-like classes. If your class wraps a list or dict, you can delegate to the underlying container:

python
class Stack: def __init__(self): self._items = [] def __len__(self): return len(self._items) stack = Stack() print(bool(stack)) # False

This makes the class behave naturally with if stack:.

Readability and Explicit Checks

Using truthiness in if items: is usually more readable than writing if len(items) > 0:. The performance difference is rarely important in ordinary code, so choose the version that expresses the intent clearly.

Explicit checks are better when you need to distinguish between None and an empty value, such as when 0 or an empty list is a valid result. With a custom object whose __bool__ or __len__ is surprising, you can also check attributes directly. The key is to choose the approach that best expresses the intent of your code.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.