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.

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.
print(bool(0)) # False
print(bool(1)) # True
print(bool([])) # False
print(bool([1, 2])) # TrueMost 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:
| Value | Example |
|---|---|
None | bool(None) is False |
False | bool(False) is False |
| Zero numeric values | 0, 0.0, 0j, Decimal(0), Fraction(0, 1) |
| Empty sequences | '', b'', (), [] |
| Empty mappings | {} |
| Empty sets | set() |
Custom objects with __bool__ returning False | Custom classes can define this |
Custom objects with __len__ returning 0 | If __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.
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.
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)) # TrueImplementing __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:
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:
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:
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:
def find_user(id):
# returns None if not found, otherwise a User object
...
user = find_user(42)
if user is not None:
# process userAnother 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.
class Configuration:
def __init__(self, settings):
self.settings = settings
def __bool__(self):
return bool(self.settings.get('enabled', False))
config = Configuration({})
print(bool(config)) # FalseOverriding __len__ is useful for collection-like classes. If your class wraps a list or dict, you can delegate to the underlying container:
class Stack:
def __init__(self):
self._items = []
def __len__(self):
return len(self._items)
stack = Stack()
print(bool(stack)) # FalseThis 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 toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.