HOWTO · Python

How to Check if a Variable Is None in Python

In this tutorial we will discuss how to test if a variable is None in Python.

On this page

Python’s None value represents the absence of a value. It is distinct from 0, False, and a string with no characters. To check whether a variable holds it, use value is None; use value is not None for the opposite condition.

Use the is Operator to Check if a Variable Is None in Python

None is a singleton, so there is one None object. The is operator checks whether two references point to the same object, which makes it the recommended direct check. This example shows both conditions:

value = None

if value is None:
    print("The value is None")
else:
    print("The value is not None")

if value is not None:
    print("A value is present")
else:
    print("No value is present")
The value is None
No value is present

Use the second form when continuing only if a value is present, such as before calling a method on it. Identity also avoids treating a different object as None just because that object’s truth value is false.

Use the isinstance() Function to Check if a Variable Is None in Python

isinstance() checks whether an object belongs to a given type and returns a Boolean. Since type(None) is NoneType, this expression tests for that type:

value = None
print(isinstance(value, type(None)))
True

This is valid when a type check is specifically useful, and the related Python isinstance() Function guide explains the function further. For a direct None check, value is None is simpler and expresses the intended identity comparison directly.

Use a Dictionary to Check if a Variable Is None in Python

A dictionary can use None as a key, so membership can find that key in a particular mapping. This is a narrow lookup example, not a general way to inspect arbitrary variables:

value = None
messages = {None: "The value is None"}

if value in messages:
    print(messages[value])
else:
    print("No matching key")
The value is None

The condition asks whether value matches a key in messages. It tells you that the value is None only because this mapping contains None and the example uses it for that purpose. For a normal variable check, use is None instead.

Use the try and except Block to Check if a Variable Is None in Python

Exception handling is useful when an operation can fail for a value such as None; it does not directly test the variable. For example, adding None to an integer raises TypeError. Catch the specific error you expect rather than using a bare except, which could hide an unrelated problem. See Python Try Except for more on exception handling.

value = None

try:
    result = value + 5
except TypeError:
    print("This operation needs a numeric value")
This operation needs a numeric value

This pattern responds to a failed addition. If your logic needs to decide whether the variable is None, check it explicitly before performing the operation.

Use the Equality (==) Operator to Check if a Variable Is None in Python

value == None compares values and evaluates to True for None. However, an object’s equality method can define custom comparison behavior, so equality is not the clearest way to identify the None singleton. Ordinary values such as 0, False, "", and [] do not compare equal to None; their truth values do not make them None. Prefer value is None for this check.

Conclusion

Use value is None to check directly whether a Python variable holds None, and value is not None when you need the converse. isinstance(value, type(None)) is an available type check, dictionary membership applies only to a specific mapping, and try/except is for handling an operation that raises an expected error. These distinctions help select the right approach for the code you are writing.