5.8 NoneType
Python's None singleton, why it's distinct from 0 and an empty string, and why you should always compare to None with is, not ==.
None Object
What Is It?
Python’s singleton “no value” object — there is exactly one None in a running program, and its type is NoneType.
Why Is It Used?
To explicitly represent the deliberate absence of a value, distinct from any actual data value like 0 or an empty string.
>>> n = None
>>> type(n)
<class 'NoneType'>
>>> n is None # always compare to None with 'is', not '=='
True
None vs 0 vs Empty String
What Is the Difference?
All three are “falsy” in a boolean context, but they mean different things. 0 is a valid number, "" is a valid (if empty) string — None means no value was ever set at all. Conflating them is a common source of bugs (e.g. treating a legitimate 0 balance as “missing data”).
>>> bool(None), bool(0), bool("")
(False, False, False) # all falsy, but NOT equal to each other
>>> None == 0, None == ""
(False, False)
Quick Interview Answer
“
Noneis Python’s singleton for ’no value’ — there’s exactly oneNoneobject in a running program, of typeNoneType. It’s falsy, but it isn’t0or\"\"— those are valid, real values. Always compare toNonewithis, not==, sinceischecks identity against that one singleton object rather than relying on equality logic.”
Common Mistakes
- Comparing to
Nonewith==instead ofis—isis both faster and the semantically correct check for a singleton. - Treating
None,0, and""as interchangeable “empty” values — they’re all falsy but represent genuinely different things. - Using a mutable default of
Noneincorrectly, or conversely forgettingNoneis the standard sentinel for “no default was given” (see 5.15 Common Mistakes for the mutable-default-argument bug).
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form