None and Truthiness
Python · Reference cheat sheet
None and Truthiness
Python · Reference cheat sheet
📋 Overview
None means “no value” (Python’s null). Truthiness means how values behave in if / while without an explicit comparison: some values count as false, everything else as true.
🔧 Core concepts
| Idea | Detail |
|---|---|
None | Singleton object; type is NoneType |
| Test for None | Use x is None / x is not None |
| Falsy values | None, False, 0, 0.0, "", [], \{\}, set(), … |
| Truthy values | Non-empty containers, non-zero numbers, most objects |
bool(x) | Explicit conversion to True/False |
Functions that “return nothing” actually return None.
💡 Examples
None as a missing value:
user = None
if user is None:
print("Please log in")
else:
print(user)Default then assign:
def find_user(user_id):
# pretend lookup failed
return None
result = find_user(42)
name = result if result is not None else "anonymous"
print(name)Truthiness in conditions:
items = []
text = ""
count = 0
if not items:
print("list is empty")
if not text:
print("no text")
if count:
print("never runs for 0")Truthy gotchas with numbers and containers:
print(bool(0), bool(1), bool(-1)) # False True True
print(bool(""), bool("0")) # False True
print(bool([]), bool([0])) # False True
print(bool(None)) # False⚠️ Pitfalls
if x == Noneworks but style guides preferif x is None.- Empty containers are falsy — that is often useful, sometimes surprising.
- The string
"False"and the number0inside a list[0]are truthy as containers/strings. - Returning
Noneaccidentally from a function that should return data is a common bug.