How To Compare Values To None In Python

How to compare values to None in Python

If you are looking for the solution to the question “How to compare values to None in Python?”, let’s read our article. In this tutorial, we will show you three ways to make the comparison between a value and a None type: using the keyword “is” or “is not”, using != and == operators and using __eq__() function.

How to compare values to None in Python?

In the Python programming language, we all know that the None type is a particular data type because it differs from other data types – common using data. Because of that, we can not make a comparison between the None type and the others in a usual way, such as smaller or greater, value is equal or not. 

When comparing, we must consider whether they are the same type or not. Keep going on. Now we will discover some ways to compare values to None. 

Using the keyword “is” or “is not”

“Is” and “is not” operators compare the identity of the target objects. The system will find and compare the address in the memory location; this can be considered the same pointer in the C programming language. 

“Is” and “is not” return the boolean value. If the target objects have the same identity (point to the same address in the memory location), the result is True and False otherwise. 

Code:

# Declare a None variable
myNone = None
 
# Declare a String variable
myStr = "This is a string"
 
# Compare by is operator
print(myNone is myStr)
 
# Compare by is not operator
print(myNone is not myStr)

Result:

False
True

Using != and == operators

!= and == operators compare the value of the target objects. It is much simpler than the “is” and “is not” operators. In practice, the objects are deposit cabinets, “is” and “is not” are the position of the box, while “!=” and “==” operators are the contents of the box. 

The result of the != and == operators are also boolean values. If the values of the target objects are the same, the result is True, and the result returns False otherwise.

Code:

# Declare a None variable
myNone = None
 
# Declare a String variable
myStr = "This is a string"
 
# Compare by != operator
print(myNone != myStr)
 
# Compare by == operator
print(myNone == myStr)

Result:

True
False

Using __eq__() function

__eq__() is also called rich comparison. It is similar to the == operator. By default, the function is available in all objects and is built based on the “is” operator. The result returns True and False. In some cases, if two objects are different types, the result will be NotImplemented.

Code:

# Declare a None variable
myNone = None
 
# Declare a String variable
myStr = "This is a string"
 
# Compare by __eq__()
print(myNone.__eq__(myStr))

Result:

NotImplemented

Summary

Our article shows you how to compare values to None in Python. Each method has its character, but they all have the same purpose. We hope that you get more knowledge after reading our article. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *