One of the data types provided by Python is the NoneType which you might have found when coding in Python if you have seen the keyword None.
NoneType is a Python data type that shows that an object has no value. None is the only object of type NoneType and you can use it to show that a variable has no value. None can also be used when a function doesn’t return a value or for defining an optional parameter in a function.
Are you ready to learn how to use None in this tutorial?
What Is NoneType in Python?
The NoneType in Python is a data type that represents the absence of a value or a null value. The None object is the only Python object of type NoneType.
Using the type() function in the Python shell we can see that None is of type NoneType.
Now try to assign the value None to a variable. You will see that the type of that variable is also NoneType.
>>> x = None
How to Check For NoneType in Python?
How can you check if a Python object is of type NoneType or, in other words, if the value of the object is equal to None?
To verify if a Python object is equal to None you can use Python’s “is operator”. You can also apply its negation using the “is not operator”.
For example, let’s take a variable called number and assign the value None to it.
Then use an if / else statement and the is operator to check if the value of this variable is None or not.
>>> number = None
>>> if number is None:
... print("The variable number is equal to None")
... print("The variable number is not equal to None")
The variable number is equal to None
Using the is operator we have correctly detected that the value of the variable number is None.
But, how does this work exactly?
To understand that we will simply check the value returned by the expression that uses the is operator.