Lesson explanation
After this lesson, you will be able to:
False, None, zero, empty text, and non-empty values.not for presence or emptiness.bool().0 separate from genuinely missing data.Python conditions do not require a value to be literally True or False. Many values have an implied truth value.
Common falsy values include:
False
None
0
0.0
""Most other values are truthy, including non-empty strings and non-zero numbers.
This makes direct checks concise:
if api_token:
print("Token available")Use not when you want the empty or missing path:
if not api_token:
print("Token missing")You can call bool(value) when you want to inspect the truth value directly. But direct truthiness is not always the right test. If 0 is a valid configured value, if not retry_limit: would treat it the same as None. In that case, check specifically for missing data with is None.
💡 Tip: Use truthiness for “does this value have content?” Use
is Nonefor “was no value provided?”
An empty string is falsy, so the fallback branch runs.
release_note = ""
if release_note:
print("Note ready")
else:
print("No release note")Output:
No release notenot for the empty pathnot reverses the truth value.
api_token = ""
if not api_token:
print("Authentication unavailable")Output:
Authentication unavailablebool()bool() makes Python's interpretation visible.
print(bool(0))
print(bool(""))
print(bool("worker"))
print(bool(3))Output:
False
False
True
TrueA zero timeout may be intentional, while None means no value was supplied.
timeout_seconds = 0
if timeout_seconds is None:
print("Timeout: default")
else:
print(f"Timeout: {timeout_seconds}")Output:
Timeout: 0is None check with a broad falsy check when the distinction matters."False" with the boolean False; non-empty text is truthy.Python for Developers
Assembled code
Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.
0 / 5 tasks completed
| Value | Truth value |
|---|
False, None | Falsy |
0, 0.0 | Falsy |
"" | Falsy |
| Non-empty text | Truthy |
| Most non-zero numbers | Truthy |