Use “is operator” to check if the value is none in Python, like this:-
if variable is None:
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.
Example check if the value is none Python
Simple example code. If the variable type is None then print its value and type.
a = None
if a is None:
print(a)
print(type(a))
Output:
How to Python check if an object is None
Example code of test if the object is NoneType python.
if variable is None:
Complete code.
a = None
if a is None:
print("I am NO ONE")
Output: I am NO ONE
Note:
None
is a singleton object of theNoneType
class.None
is not equal to anything except itself.- Use
is
oris not
operator to compareNone
with other values.
Do comment if you have any doubts and suggestions on this Python None 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.