Skip to content

Module 5 · Conditional Statements — topic 9 of 10

Truthy and falsy values

Python treats every value as true or false in a condition. Learn which values are falsy (0, empty collections, None, False), how bool() reveals truthiness, and when relying on it is a good idea.

Beginner 10 min read·Lesson 68 of 306 in Python Programming

Learning objectives

  • List Python’s falsy values
  • Use truthiness to test for empty collections
  • Avoid truthiness when 0 or empty strings are valid data

Definition

Python treats every value as true or false in a condition. Learn which values are falsy (0, empty collections, None, False), how bool() reveals truthiness, and when relying on it is a good idea.

Introduction

An if condition doesn’t have to be a comparison. Python can test any value: if items: runs the block when the list has items. Values that count as false are called falsy; all others are truthy.

Why it matters

Idiomatic Python uses truthiness constantly: if name:, while queue:, return data or []. Reading and writing it fluently makes code concise. Knowing its limits prevents bugs where a legitimate zero is mistaken for “missing”.

Detailed explanation

Falsy values: False, None, numeric zeros (0, 0.0, 0j), and empty collections and strings ("", [], (), {}, set(), range(0)).

Everything else is truthy, including negative numbers, the string "0", the string "False" and a list containing an empty list.

bool(value) shows how Python will treat a value in a condition.

Typical idioms: if not text: for empty input; if items: for non-empty lists; while queue: to process until empty.

Truthiness is a poor fit when falsy values are valid data. A score of 0 or a quantity of 0 should not be treated as missing; test is None explicitly instead.

Objects you define in classes are truthy by default; classes can change this with special methods (Module 11).

Truthiness also explains why while True: loops run until a break and why an if around a function call that returns None never runs its block: the None result is simply falsy.

Syntax

Python
if value:
handle_non_empty()
if not value:
handle_empty()
  • Any value can be used as a condition.
  • Empty and zero values are falsy.
  • Use is None when 0 or "" are meaningful.

Code examples

Testing truthiness

Python
for value in [0, 7, "", "0", [], [0], None, -1]:
print(repr(value), bool(value))
Output
0 False
7 True
'' False
'0' True
[] False
[0] True
None False
-1 True

Empty input check

Python
name = input("Name: ").strip()
if not name:
name = "Guest"
print("Welcome,", name)
Input
   
Output
Name: Welcome, Guest

More examples

Processing a queue until it is empty

Python
tasks = ["backup", "email", "report"]
while tasks:
current = tasks.pop(0)
print("Doing", current, "-", len(tasks), "left")
print("Queue empty:", not tasks)
stock = {"pens": 0, "pads": None}
for item, qty in stock.items():
if qty is None:
print(item, "count unknown")
elif not qty:
print(item, "out of stock")
Output
Doing backup - 2 left
Doing email - 1 left
Doing report - 0 left
Queue empty: True
pens out of stock
pads count unknown

Step-by-step explanation

  • while tasks: keeps looping while the list is non-empty.
  • pop(0) removes the first task each time, so the loop eventually ends.
  • not tasks is True once the list is empty.
  • is None separates an unknown count from a real zero before the truthiness test.

Expected output

Running “Processing a queue until it is empty” prints exactly:

TextOutput
Doing backup - 2 left
Doing email - 1 left
Doing report - 0 left
Queue empty: True
pens out of stock
pads count unknown

Practical applications

  • Form validation rejects empty fields with if not value.
  • Background workers loop while a job queue has items.
  • Functions return results or [] so callers can test with if results.

Best practices

  • Use if items: and if not text: for empty checks.
  • Use is None when zero or empty values are meaningful.
  • Check bool(x) in the shell when unsure.

Common mistakes

Treating zero as missing

Avoid

if not discount:
discount = 10 # overwrites a real 0

Prefer

if discount is None:
discount = 10

0 is falsy, so the first version replaces an intentional zero discount.

Comparing with len() unnecessarily

Avoid

if len(items) == 0:

Prefer

if not items:

Both work, but the truthiness form is the common Python idiom.

Key takeaways

  • Falsy: False, None, zeros and empty collections or strings.
  • Everything else is truthy, including "0" and "False".
  • Use explicit is None checks when 0 is valid.

Try it yourself

  1. 1
    Predict bool() for " ", 0.0, [[]], {} and "None", then check.
  2. 2
    Write a loop that keeps asking for input until the user enters something non-empty.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic