Open In App

How to check NoneType in Python

Last Updated : 30 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Checking NoneType in Python means determining whether a variable holds the special None value, which represents the absence of a value or a null reference. For example, a variable returned from a function might be None if no result is found. It's important to identify NoneType correctly, especially when working with optional values, function returns or input validation.

Using is operator

is operator is the most Pythonic way to check for None, as it tests identity. Since None is a singleton, this method is accurate, efficient and preferred in conditionals and return checks.

Python
x = None
if x is None:
    print("x is None")
else:
    print("x is not None")

Output
x is None

Explanation: x is None checks if x refers to None. Since x is set to None, it prints "x is None". Otherwise, it prints "x is not None".

Using is not

is not operator checks that a variable is not None. It's efficient and commonly used for input validation or confirming values before proceeding, promoting clear and readable code.

Python
x = "Hello"
if x is not None:
    print("x has a value")
else:
    print("x is None")

Output
x has a value

Explanation: x is not None checks if x is not None. Since x is "Hello", it prints "x has a value" otherwise, it would print "x is None".

Using == or != Operator

x == None checks value equality, not identity and can be overridden in custom classes, making it less safe than is. It may work in simple cases but isn't the recommended practice.

Python
x = None
if x == None:
    print("x is None")
else:
    print("x is not None")

Output
x is None

Explanation: x == None checks value equality, not identity. It works in simple cases but can be unreliable if == is overridden. Using is is safer and preferred.

Using type()

Checking for NoneType explicitly is rarely needed for simple value checks but can be useful in debugging, logging or type validation where distinguishing types matters.

Python
x = None

if type(x) is type(None):
    print("x is of NoneType")
else:
    print("x is not NoneType")

Output
x is of NoneType

Explanation: type(x) is type(None) checks if x is exactly of NoneType. Since x is None, the condition is True and it prints "x is of NoneType".

Using boolean context

None is falsy in Python, so it evaluates to False in conditionals. While concise, this isn't specific—other values like 0, '', and [] are also falsy. Use only when exact distinction from None isn't needed.

Python
x = None

if x:
    print("x is truthy")
else:
    print("x is falsy (possibly None)")

Output
x is falsy (possibly None)

Explanation: None is falsy in boolean context, so the else block runs. But since other values like 0, '' and [] are also falsy, this method isn’t reliable for checking None specifically.

Related Articles


Similar Reads