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.
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
- 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
0 False 7 True '' False '0' True [] False [0] True None False -1 True
Empty input check
Name: Welcome, Guest
More examples
Processing a queue until it is empty
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:
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
Prefer
0 is falsy, so the first version replaces an intentional zero discount.
Comparing with len() unnecessarily
Avoid
Prefer
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
- 1Predict bool() for " ", 0.0, [[]], {} and "None", then check.
- 2Write 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?