Truthy and Falsy
phrase
say it: “TROO-thee and FAWL-see” /ˈtruːθi ænd ˈfɔːlsi/
How Python treats any value in an if: empty or zero values such as "", [], 0 and None count as false (falsy), and almost everything else counts as true (truthy).
What do truthy and falsy mean?
When you put a value in an if, Python treats it as true or false. Empty or zero values ("", [], 0, None) are falsy; almost everything else is truthy.
cart = []
if cart:
print("Ready to check out")
else:
print("Your cart is empty") # this one runs
In a sentence
- “Because an empty list is falsy, if cart: skipped the checkout.”
- “A non-empty name is truthy, so if name: ran the greeting.”
Level: Novice · More Python terms at this level
Updated