Use is
operator to check NoneType in Python, like the below code. Since None
is the sole singleton object of NoneType
in Python, we can use is
operator to check if a variable has None
in it or not.
if variable is None:
Note: if you declare two or more variables with the value None, they will refer to the same NoneType object.
How to check NoneType in Python
A simple example code determining a variable’s type is NoneType in Python.
x = None
if x is None:
print(x)
print(type(x))
else:
print("X is not None")
Output:
Do comment if you have any doubts or suggestions on this Python NoneType topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
He sounds like that is the only way to do it, here are other ways:
1. == None
2. str() == “None”
3. type() == NoneType