Booleans & Truthiness
Truthiness is Python's rule that every value is treated as either "truthy" or "falsy" in a boolean context, so any object — not just True and False — can control an if or while .
Learn Booleans & Truthiness in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Understanding which values count as falsy, and how and / or actually behave, unlocks some of the cleanest, most readable patterns in the language.
Pass any object to bool() and Python tells you whether it's truthy or falsy. There's a short, fixed list of falsy things — everything else is truthy:
Because empty collections are falsy, you can test them directly. This is more readable and more idiomatic than comparing lengths:
Prefer if cart: over if len(cart) > 0: and if not name: over if name == "": . Shorter, clearer, and it works for every kind of collection.
This surprises newcomers: and and or don't return True / False — they return one of the actual operands, and they stop early ("short-circuit") as soon as the answer is known:
any() asks "is at least one truthy?" and all() asks "are they all truthy?". Pair them with a generator expression for clean validation:
all([]) being True reads as "no item broke the rule", and any([]) being False reads as "no item satisfied it". Keep that edge case in mind for empty inputs.
Replace each ___ with any , all , or , or not so the output matches.
✅ Only the empty string "" is falsy. Compare values explicitly ( flag == "False" ) or convert properly.
✅ 0 is falsy, so or discards it. When zero/empty is legitimate, test is None instead: qty if qty is not None else 1 .
✅ all() of an empty iterable is True . Guard with if items and all(...) when emptiness should fail.
Build a tiny validator that uses truthiness, all() , and an or default.
Go deeper with the official Python documentation:
Lesson complete — truthiness demystified!
You know the falsy values cold, you can test collections directly with if items: , you understand that and / or return operands and short-circuit, and you can validate with any() and all() .
🚀 Up next: None & NoneType — Python's way of saying "nothing here", and why is None beats == None .
Practice quiz
Which of these is a FALSY value in Python?
- The string '0'
An empty list [] is falsy. The string '0', the list [0], and the string 'False' are all non-empty, so they are truthy.
What does bool('0') return?
- False
- True
- 0
- None
Answer: True. '0' is a non-empty string, so it is truthy — bool('0') is True. Only the empty string '' is falsy.
What does the expression 0 or 'fallback' evaluate to?
- True
- 0
- 'fallback'
- False
Answer: 'fallback'. or returns the first truthy operand; 0 is falsy, so it returns 'fallback'. This is the common default-value pattern.
What does 'a' and 'b' evaluate to?
- True
- 'a'
- 'b'
- False
Answer: 'b'. and returns the first falsy operand, or the last one if all are truthy. Both are truthy, so it returns the last: 'b'.
What does 0 and 'b' evaluate to?
- 0
- 'b'
- True
- False
Answer: 0. and short-circuits at the first falsy operand and returns it. 0 is falsy, so it returns 0 without evaluating 'b'.
What is the result of any([]) on an empty list?
- True
- False
- None
- It raises an error
Answer: False. any([]) is False — with no items, nothing is truthy, so any() returns False.
What is the result of all([]) on an empty list?
- True
- False
- None
- It raises an error
Answer: True. all([]) is True — with no items, nothing breaks the rule, so all() vacuously returns True.
Why can 'qty or 1' be a bug when qty is 0?
- 0 raises a TypeError
- 0 is falsy, so or discards it and returns 1 even though 0 was a real value
- or only works on strings
- It always returns True
Answer: 0 is falsy, so or discards it and returns 1 even though 0 was a real value. 0 is falsy, so 'qty or 1' gives 1 even when 0 is a legitimate value. Use 'qty if qty is not None else 1' instead.
What is the Pythonic way to check that a list 'cart' is non-empty?
- if len(cart) > 0:
Because empty collections are falsy, 'if cart:' is the idiomatic, readable test for a non-empty collection.
What does 1 and 2 and 3 evaluate to?
- 1
- True
- 3
- 6
Answer: 3. When every operand is truthy, and returns the LAST one, so 1 and 2 and 3 evaluates to 3.