How to use the None Keyword in Python

The keyword None in Python is for defining a null object or variable. The datatype of something assigned None will be a NoneType object.

 

Why is it useful? In a == or is equality condition, None always returns False unless it is compared to another None. This provides a definite way of determining if something is not equal to None.

 

Here are some examples to demonstrate this:

 

print(0 == None) # returns False
print('' == None) # returns False
print(False == None) # returns False
print(None == None) # returns True

 

When using the == operator 0, an empty string and False are not equal to None.

 

Checking if a Value is None

Let's begin by checking if a value is None in a conditional if expression.

 

thing = None

if thing is None:
   print(True)
True

 

Here is another example using == (equality operator.)

 

thing = None

if thing == None:
   print(True)
True