None & NoneType
Published by Brayan K
None is Python's special constant that represents the deliberate absence of a value — the single instance of a type called NoneType that means "nothing here".
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Missing is different from zero
Suppose a sensor has not supplied a reading yet. None can represent that absence, while zero can represent a real measurement. A condition that rejects every falsy value would mix those cases together. Use is None when you mean that particular absence marker.
for value in [None, 0, False, ""]:
print(repr(value), value is None)
print(type(None).__name__)
# ✅ Expected output:
# None True
# 0 False
# False False
# '' False
# NoneTypeNone is a single object, so the identity test is appropriate. The word None inside quotes is just text. A name containing None is still a defined name; deleting the name would be a different operation. Do not add a numeric fallback until you have decided what an absent reading means for your program.
The other values that count as false are covered in Booleans & Truthiness. Here you will preserve those values while handling absence explicitly.
An error mentioning NoneType points to a missing value
None is not an empty number that arithmetic automatically treats as zero. This increment fails because the reading is absent. The error names NoneType, the type of the value involved; it does not mean you need to import or install a type.
reading = None
print(reading + 1)
# ✅ Expected error:
# TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'Handle absence before performing the operation. The corrected program reports the missing reading and still increments a real zero. Replacing every missing reading with zero would be a different policy and could hide a sensor failure.
for reading in [None, 0, 4]:
if reading is None:
print("no reading")
else:
print(reading + 1)
# ✅ Expected output:
# no reading
# 1
# 5When None is unexpected, trace the value back to the operation that supplied it. That is often more useful than changing the line where the error finally occurs. A function may have a branch with no return value, or a method may change an object instead of returning a replacement.
Functions in Python owns return statements and fall-through returns. Lists in Python explains why in-place list methods such as sort() return None; keep the list and inspect the method result separately.
A missing key and a stored None can look identical
A dictionary can explicitly store None. Calling get() with no default returns None both for that stored value and for a missing key. If those cases matter separately, check whether the key exists. Do not interpret the value alone as proof that the field was omitted.
omitted = {}
explicit = {"nickname": None}
print(omitted.get("nickname") is None)
print(explicit.get("nickname") is None)
print("nickname" in omitted)
print("nickname" in explicit)
# ✅ Expected output:
# True
# True
# False
# TrueFor an update form, omitting a field might mean “leave the old value alone”, while explicitly sending None might mean “clear it”. Choose and document that contract. The lookup syntax itself is taught in Dictionaries in Python.
An empty default list is shared across calls
A default argument is created when the function definition runs, not afresh for each call. If the default is a list and the function changes it, a later call that omits the argument sees that changed list. The second call below is not starting a new notebook:
def add_note(text, notes=[]):
notes.append(text)
return notes
print(add_note("read"))
print(add_note("draft"))
# ✅ Expected output:
# ['read']
# ['read', 'draft']Use None as the default when it can unambiguously mean “no list was supplied”. Then create the list inside the call, only for that case. A caller who supplies a list keeps control of the actual object passed in.
def add_note(text, notes=None):
if notes is None:
notes = []
notes.append(text)
return notes
print(add_note("read"))
print(add_note("draft"))
notebook = []
returned = add_note("edit", notebook)
print(notebook)
print(returned is notebook)
# ✅ Expected output:
# ['read']
# ['draft']
# ['edit']
# TrueThe is None check is essential. Replacing it with notes = notes or [] would also discard a caller-supplied empty list, because that list is falsy. This version appears to work until the caller checks their own notebook:
def add_note(text, notes=None):
notes = notes or []
notes.append(text)
return notes
notebook = []
returned = add_note("edit", notebook)
print(notebook)
print(returned)
print(returned is notebook)
# ✅ Expected output:
# []
# ['edit']
# FalseThe earlier explicit check preserves the supplied empty list and fixes this behaviour. If None itself is a valid argument whose meaning differs from omission, it cannot represent both cases. You need a separate sentinel object or an explicit flag; do not silently assign two meanings to the same marker.
Apply a default without discarding zero
A retry limit of zero means “do not retry”. An absent limit should use three. Fill the comparison and fallback blanks, then confirm that a real zero survives. The comparison must ask about None, not about general truthiness.
for configured in [None, 0, 2]:
if configured ___ None:
effective = ___
else:
effective = configured
print(effective)
# 👉 replace ___ with is
# 👉 replace ___ with 3
# ✅ Expected output:
# 3
# 0
# 2Find the lowest available reading
Given the readings below, discard only the absent values, then print the lowest remaining reading. If every reading was absent, print a message instead of calling min() on an empty list. Try both input lists. A real zero must remain eligible to be the minimum.
batches = [[None, 0, 8, None, 3], [None, None]]
for readings in batches:
available = [value for value in readings if value is not None]
if available:
print(min(available))
else:
print("no readings")
# ✅ Expected output:
# 0
# no readings
Practice quiz
What does this program print? quantity = 0 print(quantity is None)
- True
- False
- None
- A TypeError
Answer: False. Zero is a real numeric value, not the absence marker.
What does this program print? print(type(None).__name__)
- None
- null
- bool
- NoneType
Answer: NoneType. None is the sole instance of NoneType.
What does this program print? print(None == "None")
- False
- True
- None
- A NameError
Answer: False. The string contains four characters; it is not the None object.
What does this program print? settings = {"limit": None} print(settings.get("limit") is None, "limit" in settings)
- False True
- True False
- True True
- False False
Answer: True True. The key exists even though its stored value is None.
What does this program print? settings = {} print(settings.get("limit") is None, "limit" in settings)
- True True
- True False
- False True
- False False
Answer: True False. get returns its default None, but membership reveals that the key is absent.
What does this program print? values = [None, 0, 2] print([value for value in values if value is not None])
- [2]
- [None, 0, 2]
- [0]
- [0, 2]
Answer: [0, 2]. An explicit identity test keeps zero.
Why is notes=[] a risky default when the function appends to notes?
- The default list is created once and reused when the argument is omitted
- Python forbids lists as defaults
- Every list starts with None
- Appending changes the function name
Answer: The default list is created once and reused when the argument is omitted. A default is evaluated when the definition runs, so later calls can share the mutated list.
A function should append to the exact list the caller supplies, including an empty list. Which check creates a new list only when no list was supplied?
- if not notes:
- notes = notes or []
- if notes is None:
- if notes == False:
Answer: if notes is None:. The explicit None sentinel preserves a supplied empty list.
Continue this course
- Previous: Booleans & Truthiness
- Next: Conditional Expressions (Ternary) — Pick a value inline with a if cond else b, including in comprehensions
- Quick reference: Python cheat sheet › Data Types