Sub topic 7
None: Python's absence of a value
This chapter assumes you know basic Python syntax (variables, functions, if statements) and the built-in types int, str, list, and dict. It covers what None is, where it shows up, and how it differs from the other "empty" values. Later chapters cover is versus == and default arguments in more detail.
What None is
None is Python's way of saying "there is no value here." It is the only value of the type NoneType. Python creates exactly one None object for the whole program, so it is called a singleton.
print(None) # None
print(type(None)) # <class 'NoneType'>
print(bool(None)) # FalseYou cannot create a second None. Calling type(None)() returns the same single object.
Where None comes from
You will meet None in several places, often without writing it yourself.
- A function with no
returnstatement returnsNone. - A bare
returnreturnsNone. - Methods that change an object in place, such as
list.sort()andlist.append(), returnNone. - Lookups that may find nothing, such as
dict.get()andre.match(), returnNoneon a miss.
def greet(name):
print("Hello,", name)
result = greet("Ada") # prints: Hello, Ada
print(result) # None
numbers = [3, 1, 2]
sorted_result = numbers.sort()
print(sorted_result) # None
print(numbers) # [1, 2, 3]
config = {"host": "localhost"}
print(config.get("port")) # NoneThe numbers.sort() line is a common mistake. People write numbers = numbers.sort() and lose their list, because sort() returns None. Use sorted(numbers) when you want a new list back.
flowchart TD
A[Call a function or method] --> B{Does it return a value?}
B -->|Yes| C[You get that value]
B -->|No return statement| D[You get None]
B -->|Lookup finds nothing| D
B -->|In-place change| DThe block above is only a sketch of the idea. The proper diagram follows.
None is not the same as "empty"
None is falsy, as are 0, "", and []. That does not make them the same thing. Each of them is a real value of its own type. None means no value was given at all.
values = [None, 0, "", [], {}, False]
for v in values:
print(repr(v), bool(v), v is None)Every line prints False for the truth value, but only the first prints True for v is None. The practical consequence is that if not value cannot tell a missing value from a legitimate zero or empty string.
| Value | Type | `bool(value)` | Means |
|---|---|---|---|
| `None` | `NoneType` | `False` | No value |
| `0` | `int` | `False` | The number zero |
| `""` | `str` | `False` | A string with no characters |
| `[]` | `list` | `False` | A list with no items |
| `False` | `bool` | `False` | A false answer |
A concrete case: a user's age of 0 is real data, while an age of None means "not provided."
def describe_age(age):
if age is None:
return "age not provided"
return f"age is {age}"
print(describe_age(None)) # age not provided
print(describe_age(0)) # age is 0If you had written if not age:, the second call would wrongly report "age not provided."
Using None as a marker
Because None is a single, unmistakable value, programs use it as a placeholder meaning "not set yet" or "nothing found."
class Order:
def __init__(self):
self.shipped_at = None # not shipped yet
def ship(self, timestamp):
self.shipped_at = timestamp
order = Order()
print(order.shipped_at) # None
order.ship("2026-11-26")
print(order.shipped_at) # 2026-11-26Using None here is clearer than inventing a special string such as "N/A" or a number like -1, because those could be confused with real data.
Things you cannot do with None
None has no attributes or operations of its own beyond a few basics, so treating it like a real value fails. The most common error in Python programs comes from this.
import re
match = re.match(r"\d+", "abc")
print(match) # None
try:
print(match.group())
except AttributeError as e:
print("AttributeError:", e)
# AttributeError: 'NoneType' object has no attribute 'group'Similarly, None + 1 raises TypeError. When you see 'NoneType' object ... in an error message, a value you expected to exist was None. Trace back to find which call returned it.
Exercise
What do result and numbers contain after this code runs, and why?
numbers = [3, 1, 2]
result = numbers.sort()
Show answer
result is None and numbers is [1, 2, 3]. The sort() method sorts the list in place and returns None, so assigning its return value to result stores None. Use sorted(numbers) to get a new sorted list.
Exercise
A function takes discount=None. Why is if not discount: a poor way to detect that no discount was given?
Show answer
A discount of 0 is a real, valid value, but it is also falsy, so if not discount: would treat it the same as a missing value. Use if discount is None: to detect only the missing case.