Truthy and Falsy Values
"Readability counts."
— The Zen of Python
Introduction
In Python, every object has an associated truth value.
When an object is used in a conditional statement such as if, while, or with logical operators (and, or, not), Python automatically determines whether that object should be treated as True or False.
Understanding truthy and falsy values allows you to write cleaner, more Pythonic code and avoid unnecessary comparisons.
What Are Truthy and Falsy Values?
A truthy value behaves like True in a boolean context.
A falsy value behaves like False.
For example,
Output
The list itself is not True, but it is considered truthy because it contains elements.
Falsy Values
Python has only a small number of built-in falsy values.
| Value | Description |
|---|---|
False |
Boolean false |
None |
Null object |
0 |
Integer zero |
0.0 |
Floating-point zero |
0j |
Complex zero |
"" |
Empty string |
[] |
Empty list |
() |
Empty tuple |
{} |
Empty dictionary |
set() |
Empty set |
range(0) |
Empty range |
Everything else is generally considered truthy.
Truthy Values
Examples of truthy values:
Each of these evaluates to True in a boolean context.
Checking Empty Collections
Instead of writing
write
Likewise,
Instead of
write
This is shorter, more readable, and considered Pythonic.
Examples
Lists
Output
Strings
Output
Dictionaries
Output
Using not
The not operator reverses the truth value.
Output
Truthiness in Loops
A common interview pattern:
The loop automatically stops when the list becomes empty.
No explicit length check is required.
Truthiness with and and or
Python's logical operators return objects, not just True or False.
Example:
Output
Because the empty list is falsy.
Another example:
Output
The first operand is truthy, so and returns the second operand.
Why This Matters in DSA
Many interview solutions rely on truthiness.
Examples:
Checking if a stack contains elements:
Checking whether BFS should continue:
Checking for an empty string:
Checking whether a dictionary has been populated:
These patterns appear in almost every coding interview.
Time Complexity
Truth value testing is generally O(1) for Python's built-in data types.
Checking
does not iterate through the list.
Common Interview Problems
Truthiness appears in:
- DFS
- BFS
- Stack problems
- Queue problems
- Binary Trees
- Linked Lists
- Dynamic Programming
- String processing
Common Mistakes
Mistake 1
Writing
Instead write
Mistake 2
Writing
Instead write
Mistake 3
Comparing directly with True
Instead write
Mistake 4
Comparing directly with False
Instead write
Best Practices
- Prefer truthiness over explicit length checks.
- Use
if not collection:for empty collections. - Use
while queue:instead of checking the queue's length. - Avoid comparing boolean values with
== Trueor== False.
Key Takeaways
- Every Python object has a truth value.
- Empty collections are falsy.
- Non-empty collections are truthy.
Noneis falsy.- Truthiness leads to cleaner and more idiomatic Python code.
- Most interview solutions rely heavily on truthy and falsy evaluation.
Related Topics
- Type Conversion
- Operators Used in DSA
- Collections
- Stacks
- Queues
Practice Questions
- Which built-in values are considered falsy in Python?
- Why is
if nums:preferred overif len(nums) > 0:? - What does
not []evaluate to? - What does
"Python" and 42return? - What does
[] or "default"return?
Summary
Truthy and falsy values are one of Python's most useful language features.
Rather than writing verbose conditional expressions, Python allows objects to naturally express whether they should be treated as True or False.
Understanding this behavior will help you write cleaner, shorter, and more idiomatic solutions during coding interviews.