7.10 Boolean Evaluation
Truthy and falsy values in Python — the exact set of values that evaluate as False in a boolean context — and explicit bool() conversion.
What Is It?
Python treats every value as either “truthy” or “falsy” in a boolean context (if x:, while x:, bool(x)) — not just actual True/False values.
Truthy Values
Most values are truthy by default: non-zero numbers, non-empty strings/lists/dicts, and any object that doesn’t specifically define itself as falsy.
>>> bool(1), bool("a"), bool([1])
(True, True, True)
Falsy Values
A specific, memorizable set of values are falsy: 0, 0.0, "" (empty string), [] (empty list), {} (empty dict), set() (empty set), and None.
>>> bool(0), bool(""), bool([]), bool(None)
(False, False, False, False)
bool() Conversion
Explicitly converts any value to True or False using the truthy/falsy rules above — useful for normalizing a value before storing or comparing it as a boolean.
>>> bool(0), bool(1), bool(""), bool("a"), bool([]), bool([1]), bool(None)
(False, True, False, True, False, True, False)
Quick Interview Answer
“Every value in Python is truthy or falsy in a boolean context, not just literal
True/False. The falsy set is small and memorizable:0,0.0,'',[],{},set(), andNone— everything else is truthy by default. This is whyif my_list:is the idiomatic way to check a list is non-empty instead ofif len(my_list) > 0:, and whyif x:behaves differently fromif x is True:for any value that isn’t literally the booleanTrue.”
Common Mistakes
- Writing
if len(my_list) > 0:instead of the more idiomaticif my_list:— both work, but the latter is the Pythonic convention and reads more naturally. - Confusing
if x:(truthy check) withif x is True:(identity check against the literalTrue) — the second rejects any truthy value that isn’t literallyTrue, like1or"yes". - Forgetting
0and0.0are falsy when using a numeric value as a flag — a legitimate zero value can accidentally be treated as “unset.”
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form