You can check if an object is not equal to another object in Python with the not equal operator !=.
a = 0
b = 1
if a != b:
print("a is not equal to b")
#Output:
a is not equal to b
When working with objects and variables in Python, the ability to check if two variables have the same value or a different value easily is valuable.
One such case is if you want to check if a variable is not equal to another variable in Python.
You can check if an object is not equal to another object in Python with the not equal operator !=.
!= is a Python comparison operator and allows you to compare the values of objects.
Below is a simple example showing you how to check if two variables are not equal to each other in Python.
a = 0
b = 1
if a != b:
print("a is not equal to b")
#Output:
a is not equal to b
Checking if an Object is Equal to Another in Python with !=
The != operator works to check if the value of the objects is the same but doesn’t work to check if the identities and memory locations are the same like the is keyword.
Below shows an example of how != is different from is and not is in Python.
a = [0]
b = [0]
print(a == b)
print(a != b)
print(a is b)
print(a is not b)
#Output:
True
False
False
True
Hopefully this article has been useful for you to learn about the not equal operator != in Python.