In short
Python lets you write if cart: instead of if len(cart) > 0:. It reads beautifully, but only if you know which values count as true and which count as false.
Every value counts as true or false in a condition. 0, 0.0, empty strings, empty lists, empty dicts and None are falsy, and almost everything else is truthy, so if items: checks whether a list has anything in it.

Think of it like this
Shaking the box
Before opening a parcel, you shake it. Rattle? Something's inside. Silence? Probably empty. Python shakes every value the same way: an empty list, an empty string, zero or None sounds empty (falsy). Anything with something in it rattles (truthy).
Watch it happen
What counts as false?
Falsy
- False and None
- Zero: 0, 0.0
- Empty text: ""
- Empty collections: [], (), {}, set(), range(0)
Truthy
- True
- Any non-zero number, even -1 or 0.001
- Any non-empty string, even "0", " " or "False"
- Any collection with at least one item, even [0]
The rule of thumb: empty or zero is falsy, everything else is truthy.
The big ideas
What you need to know
if items: checks for content
if cart: means "if the cart has anything in it". if not name: means "if name is empty". This is the Pythonic style.
Strings like "0" are truthy
Input arrives as text, so "0" and "False" are non-empty strings and count as True. Convert or compare them first.
Careful when 0 is valid
if discount: is False for a real discount of 0. When 0 is a legitimate value, write if discount is not None: instead.
or picks a fallback
name = typed_name or "Guest" uses "Guest" when typed_name is empty, because or returns the first truthy value.
See it in code
Walk through the code
1cart = []2name = " ".strip()3coupon = None4 5if not cart:6 print("Your cart is empty")7if not name:8 print("Please enter your name")9if coupon:10 print("Coupon applied")11else:12 print("No coupon")13 14print(bool(0), bool(""), bool([]), bool({}), bool(None)) # all False15print(bool(-1), bool("0"), bool(" "), bool([0])) # all True16 17nickname = "" or "Guest"18print(nickname) # Guest1/5An empty list, an empty string (after stripping spaces) and None. All three are falsy.
All three ifs take the "empty" path. The bool() lines show the rules directly.
Save yourself the pain
Common mistakes
Oops
Writing if len(items) > 0: or if items != []: everywhere.
Do this instead
Write if items:. It works for lists, strings, dicts and sets, and PEP 8 recommends it.
Oops
Using if quantity: when 0 is a valid quantity.
Do this instead
Be explicit when zero matters: if quantity is not None:.
Oops
Checking if answer: to see if the user typed "yes".
Do this instead
Any typed text is truthy, including "no". Compare the value: if answer.lower() == "yes":.
Your mission
Predict, then check
Quick check
Which of these values is truthy?
Go deeper
- 1Print bool() of 0, '', [], {}, None and ' ' and guess before you run.
- 2Rewrite if len(items) > 0: as if items:.
- 3Be careful when 0 is a valid value, and use is None for missing values instead.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.