The Python not equal to (
left!=right) operator returns
True when its
left operand is not equal to its
right operand as defined by the
__ne__() magic method. Otherwise, it returns
False. For example,
3!=2 evaluates to
3!=3 evaluates to
Let’s explore a couple of examples regarding the not equal to operator.
Is 3 not equal to 2?
>>> 3 != 2 True
'h' not equal to
>>> 'h' != 'h' False
>>> [1, 2] != [1, 2] False >>> [1, 2] != [1, 2, 3] True >>> (1, 1) != (1, 1, 1) True >>> 'hello' != 'hello!' True
The list not equal to operator iterates over the lists and checks pairwise if the i-th element of the left operand is different to the i-th element of the right operand.
Can you use the not equal to operator on custom objects? Yes!
Python Not Equal to on Custom Objects
To use the not equal to operator on custom objects, define the
__ne__() “dunder” magic method that takes two arguments:
other. Use attributes of the custom objects to determine if an object is not equal to another. The return value is a Boolean
In the following code, you check if a Person is not equal to another Person by using the
age attribute as a decision criterion:
class Person: def __init__(self, age): self.age = age def __ne__(self, other): return self.age != other.age alice = Person(18) bob = Person(19) carl = Person(18) print(alice != bob) # True print(alice != carl) # False
Because Alice is 18 years old and Bob is 19 years old, the result of
alice != bob is
True. But the result of
alice != carl evaluates to
False as both have the same age.
Python Not Equal to Multiple Values
How to check that a variable is not equal to multiple variables?
To check whether a variable is not equal to multiple values given in a list, use the
not in membership operator. For example, the expression
'Alice' not in ['Ann', 'Bob', 'Carl'] returns
True if the string
'Alice' is not in the given list, and
The following example creates a list of three values and stores it in the variable
negatives. You then check if a query string
'Alice' is not equal to the elements stored in the list
negatives. If so, it prints
'Yay!' to the shell:
negatives = ['Ann', 'Bob', 'Carl'] if 'Alice' not in negatives: print('Yay!')
The output is:
Python Not Equal to 0
To check if a variable x is not equal to the value 0, use the expression
x != 0. In a Boolean context such as an if condition environment, you can also use the expression
if x: instead of
if x != 0: because Python implicitly transforms each 0 value into a Boolean
False, and each other value into
Here are both semantically-identical methods:
x = 42 # Method 1 if x != 0: print('yes') # Method 2 if x: print('yes')
The output of the code snippet is:
Python Not Equal to NaN
To check whether a number
x is not equal to NaN, use the
not math.isnan(x) expression that returns
True if the number
x is not NaN, and
The following code shows an example where we first create a NaN float value using the
float('nan') built-in method, and then checking that number using
math.isnan(x). The result is
False because the value is, indeed, not a number.
import math x = float('nan') print(not math.isnan(x)) # False
Comparison operators are applied to comparable objects and they return a Boolean value (
|>||Greater Than||Returns |
|<||Less Than||Returns |
|==||Equal To||Returns |
|!=||Not Equal To||Returns |
|>=||Greater Than or Equal To||Returns |
|<=||Less Than or Equal To||Returns |
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.