Skip to content

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,

if [1, 2, 3]:
    print("List is not empty")

Output

List is not empty

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:

1
-1
3.14
"Python"
[1, 2, 3]
{"name": "Alice"}
(10,)
{1, 2}
True

Each of these evaluates to True in a boolean context.


Checking Empty Collections

Instead of writing

if len(nums) > 0:

write

if nums:

Likewise,

Instead of

if len(queue) == 0:

write

if not queue:

This is shorter, more readable, and considered Pythonic.


Examples

Lists

numbers = []

if numbers:
    print("Not Empty")
else:
    print("Empty")

Output

Empty

Strings

text = ""

if text:
    print("Has content")
else:
    print("Empty")

Output

Empty

Dictionaries

student = {}

if student:
    print("Contains data")
else:
    print("Dictionary is empty")

Output

Dictionary is empty

Using not

The not operator reverses the truth value.

numbers = []

if not numbers:
    print("Empty List")

Output

Empty List

Truthiness in Loops

A common interview pattern:

stack = []

while stack:
    print(stack.pop())

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:

print([] or "default")

Output

default

Because the empty list is falsy.


Another example:

print("Python" and 42)

Output

42

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:

if stack:

Checking whether BFS should continue:

while queue:

Checking for an empty string:

if not s:

Checking whether a dictionary has been populated:

if visited:

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

if nums:

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

if len(nums) > 0:

Instead write

if nums:

Mistake 2

Writing

if len(nums) == 0:

Instead write

if not nums:

Mistake 3

Comparing directly with True

if flag == True:

Instead write

if flag:

Mistake 4

Comparing directly with False

if flag == False:

Instead write

if not flag:

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 == True or == False.

Key Takeaways

  • Every Python object has a truth value.
  • Empty collections are falsy.
  • Non-empty collections are truthy.
  • None is falsy.
  • Truthiness leads to cleaner and more idiomatic Python code.
  • Most interview solutions rely heavily on truthy and falsy evaluation.

  • Type Conversion
  • Operators Used in DSA
  • Collections
  • Stacks
  • Queues

Practice Questions

  1. Which built-in values are considered falsy in Python?
  2. Why is if nums: preferred over if len(nums) > 0:?
  3. What does not [] evaluate to?
  4. What does "Python" and 42 return?
  5. 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.