Truthiness can hide meaning
Python lets many values act like True or False in conditionals. That is convenient, but it can hide important distinctions when different falsy values mean different things.
What is happening?
All of these values are falsy:
0""[]{}None
That makes code like this easy to write:
value = 0
if not value:
print("No value")
But 0 may be a valid result rather than an absence of data.
Why this matters
The same conditional can blur together cases that should be handled differently:
0might mean a real numeric count""might mean an intentionally empty string[]might mean "present but empty"Nonemight mean "missing"
Those differences are often part of the program's logic.
Prefer explicit checks when meaning matters
If the real question is whether a value is missing, say that directly:
if value is None:
print("Missing value")
If the real question is whether a list is empty, checking truthiness may be fine:
if not items:
print("No items")
The key is to match the condition to the meaning, not just to the boolean result.
Rules of thumb
- Truthiness is convenient, but it can hide distinctions.
- Use
is Nonewhen you mean "missing." - Use truthiness checks when emptiness is the real concept.
- Be explicit when
0,"", orNoneshould mean different things.