Skip to main content

How to Check Whether a Value Is None in Python with is None

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Python uses None for "no value here": an option nobody set, a search that found nothing, a function that returns nothing. Before you use a value that might be None, check it with is None. This guide shows the check, and its opposite, on Python 3.15.2.

Syntax​

Syntax
if <value> is None:
<code to run when there is no value>

if <value> is not None:
<code to run when there is a value>
PartWhat it means
<value>Any variable or expression
is NoneTrue only when the value is the None object itself
is not NoneThe opposite: True for every other value

The Python documentation describes None as "An object frequently used to represent the absence of a value" and adds that it "is the sole instance of the NoneType type." Because there is only one None, the identity operator is is the right test: "x is y is true if and only if x and y are the same object." Python's style guide, PEP 8, says: "Comparisons to singletons like None should always be done with is or is not, never the equality operators."

The Example​

A person has no middle name, stored as None, and the nickname Sam. The script prints a line for the missing one and for the one that's there.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

none_check.py
middle_name = None
nickname = "Sam"

if middle_name is None:
print("No middle name")

if nickname is not None:
print(f"Nickname: {nickname}")

The none_check.py script in a macOS-style code window: middle_name is None and nickname is Sam; the first if prints No middle name when middle_name is None, the second prints the nickname when nickname is not None.

Step 2: Run It​

Terminal
uv run python none_check.py
Output
No middle name
Nickname: Sam

The terminal after uv run python none_check.py: No middle name, then Nickname: Sam.

middle_name is None was True, so the first message printed. nickname is not None was True too, so the second one printed with the nickname. The command works unchanged in PowerShell.

Step 3: See Why if not value Is a Different Test​

if not value: looks shorter, but it asks a different question: whether the value counts as false. None does, and so do 0, "" and []. A count of zero is a real value, not a missing one:

not_zero.py
count = 0

if not count:
print("Treated as missing")

if count is None:
print("Missing")
else:
print(f"Count: {count}")
Terminal
uv run python not_zero.py
Output
Treated as missing
Count: 0

The terminal after uv run python not_zero.py: Treated as missing from the if not count test, then Count: 0 from the is None test.

if not count took the zero for a missing value. count is None was False, so the script printed the real count, 0. PEP 8 warns about exactly this: "beware of writing if x when you really mean if x is not None".

Common Mistakes​

MistakeWhat happensDo this instead
if value == None:It works for plain values, but == asks the object, and PEP 8 says never to use it for Noneif value is None:
if not value: when 0 or "" are real valuesThey're treated as missing, as Step 3 showsif value is None:
Using the value before the check, such as len(name)TypeError: object of type 'NoneType' has no len() when it's NoneCheck is None first

FAQ​

Is is not None the same as not value is None?​

They give the same answer, but PEP 8 asks for is not, which reads as plain English.

Can I use is to compare numbers or text?​

No. is asks whether two names point to the very same object, not whether the values are equal. Use == for values, and keep is for None.

Conclusion​

middle_name is None was True for None, and nickname is not None was True for "Sam". if not count took a count of 0 for a missing value, while count is None didn't, which is why is None is the test for "no value".

Additional Resources​