Python None keyword
last modified November 29, 2026 Python 3.15
In this article we show how to use the None keyword in
Python.
None is a special value that represents the absence of a
value. It is used for variables that have no value yet, for results that
were not found, for optional arguments, and as the return value of
functions that return nothing. None is the only object of the
NoneType type, so there is exactly one None in a
running program.
Other languages use null or nil for a similar
purpose. In Python, None is an ordinary object: it can be
assigned, passed, returned, and stored in containers.
The None value
None is often assigned to a variable that will receive its
real value later.
winner = None print(winner) print(type(winner)) winner = "Klára" print(winner) a = None b = None print(a is b) print(bool(None))
Printing None shows the word None; its type is
NoneType. Because None is a singleton, two
variables set to None always refer to the same object, and
a is b is true. In a boolean context None is
false.
$ python none_value.py None <class 'NoneType'> Klára True False
Testing for None
The recommended test is the identity operator: x is None or
x is not None. Testing the truth of a value with
if not x is not the same thing, because 0, an
empty string, and an empty list are false as well.
values = [15, None, 0, "", None, 8]
for v in values:
if v is None:
print(repr(v), "-> missing")
else:
print(repr(v), "-> present")
discount = 0
if not discount:
print("not discount: treated as missing")
if discount is None:
print("is None: treated as missing")
print("discount checked")
The is None test separates the two missing values from all
the others, including 0 and the empty string. The
if not discount test treats a discount of zero as missing,
which is a common bug when zero is a valid value. Comparing with
== None also works for built-in types, but a class can define
its own equality, while is cannot be overridden.
$ python testing_none.py 15 -> present None -> missing 0 -> present '' -> present None -> missing 8 -> present not discount: treated as missing discount checked
None is a keyword
None is a keyword, like True and
False. It cannot be used as a variable name, so its value can
never change.
print("assigning to None")
None = 0
The assignment is rejected when the file is compiled. No line of the file runs, not even the first print statement.
$ python assign_none.py
File "/home/user/none-keyword/assign_none.py", line 2
None = 0
^^^^
SyntaxError: cannot assign to None
Functions return None
A function that ends without a return statement returns
None. A bare return, without an expression, also
returns None.
def greet(name):
print(f"Hello there, {name}!")
def check_age(age):
if age < 0:
return
return age >= 18
result = greet("Tomáš")
print(result)
print(check_age(20))
print(check_age(-5))
value = print("printing")
print(value)
greet prints a message and has no return
statement, so its result is None. check_age
returns None for a negative age, which tells the caller that
there is no answer; the caller should test the result with
is None, because False is also a possible
result. The built-in print function returns
None too.
$ python returns.py Hello there, Tomáš! None True None printing None
Methods that modify in place
Methods that change an object in place, such as list.sort
and list.append, return None. Assigning their
result is a common mistake.
numbers = [5, 2, 9, 1]
result = numbers.sort()
print(result)
print(numbers)
names = ["Eva", "Adam"]
names = names.append("Zoe")
print(names)
numbers = [5, 2, 9, 1]
ordered = sorted(numbers)
print(ordered, numbers)
numbers.sort() sorts the list and returns None;
the sorted values are in numbers. The assignment
names = names.append("Zoe") replaces the list with
None, and the list is lost. The built-in sorted
function, on the other hand, returns a new list and leaves the original
unchanged. Returning None from in-place methods is a
deliberate convention that marks them as having side effects.
$ python in_place.py None [1, 2, 5, 9] None [1, 2, 5, 9] [5, 2, 9, 1]
Missing values
Many functions and methods return None when there is no
result. None is also a natural initial value for a variable
that a search may or may not fill.
capitals = {"Slovakia": "Bratislava", "Austria": "Vienna"}
print(capitals.get("Austria"))
print(capitals.get("Hungary"))
print(capitals.get("Hungary", "unknown"))
scores = [72, 45, 91, 38]
first_top = None
for s in scores:
if s > 95:
first_top = s
break
print(first_top)
if first_top is None:
print("no score above 95")
dict.get returns None for a missing key instead
of raising a KeyError; a second argument replaces it with
another default. The loop searches for a score above 95.
first_top starts as None and stays
None because no score qualifies, so the final test reports
that nothing was found.
$ python missing_values.py Vienna None unknown None no score above 95
None as a default parameter
Default values of parameters are evaluated once, when the function is
defined. A mutable default, such as an empty list, is therefore shared by
all calls. None is the usual default for such parameters; the
function creates a new object when it receives None.
def add_tag(tag, tags=[]):
tags.append(tag)
return tags
print(add_tag("python"))
print(add_tag("linux"))
def add_label(label, labels=None):
if labels is None:
labels = []
labels.append(label)
return labels
print(add_label("python"))
print(add_label("linux"))
print(add_label("docs", ["draft"]))
add_tag appends to the one list created when the function
was defined, so the second call returns both tags. add_label
uses None as the default and creates a fresh list in each
call. A list passed by the caller is used as it is. When
None itself is a meaningful argument, a separate sentinel
object is needed instead; see
Python sentinel builtin.
$ python mutable_default.py ['python'] ['python', 'linux'] ['python'] ['linux'] ['draft', 'docs']
Sorting values with None
None cannot be compared with numbers or strings using
< or >. Sorting a list that contains
None fails unless a key function handles it.
ages = [34, None, 19, None, 52]
try:
print(sorted(ages))
except TypeError as e:
print("error:", e)
def none_last(age):
return (age is None, age)
print(sorted(ages, key=none_last))
known = []
for age in ages:
if age is not None:
known.append(age)
print(known, sum(known) / len(known))
Plain sorted raises a TypeError. The
none_last key function turns each value into a tuple whose
first item is False for numbers and True for
None; tuples are compared item by item, so the numbers come
first in their natural order and the None values follow. To
compute an average, the loop first collects only the known values.
$ python sorting_none.py error: '<' not supported between instances of 'NoneType' and 'int' [19, 34, 52, None, None] [34, 19, 52] 35.0
Type hints with None
In type hints, a value that may be missing is written as
T | None, for example str | None. The older
Optional[str] form from the typing module means
the same.
def find_user(user_id: int) -> str | None:
users = {1: "alice", 2: "bob"}
return users.get(user_id)
def greeting(name: str | None = None) -> str:
if name is None:
return "Hello there!"
return f"Hello there, {name}!"
for uid in [1, 3]:
user = find_user(uid)
if user is not None:
print(user.upper())
else:
print(f"user {uid} not found")
print(greeting())
print(greeting("Peter"))
find_user returns a name or None. A type
checker requires the caller to test for None before calling
user.upper(); without the test, the call would fail at
runtime with an AttributeError for a missing user. The
greeting parameter is optional and defaults to
None.
$ python type_hints.py ALICE user 3 not found Hello there! Hello there, Peter!
Source
The None constant, None in the data model, Function definitions and default values, PEP 604 – Union types as X | Y
In this article we have looked at the None value, tested for it with is, seen that functions and in-place methods return None, used None for missing values and default parameters, sorted data containing None, and written type hints for optional values. See also Python sentinel builtin, Python data types, and Python functions.
Author
List all Python tutorials.