Python Not Equal To

Rate this post

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 True, but 3!=3 evaluates to False.

Examples

Let’s explore a couple of examples regarding the not equal to operator.

Is 3 not equal to 2?

>>> 3 != 2
True

What about 'h' not equal to 'h'?

>>> 'h' != 'h'
False

Can you compare collections such as lists, strings, tuples?

>>> [1, 2] != [1, 2]
False
>>> [1, 2] != [1, 2, 3]
True
>>> (1, 1) != (1, 1, 1)
True
>>> 'hello' != 'hello!'
True

Yes!

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: self and other. Use attributes of the custom objects to determine if an object is not equal to another. The return value is a Boolean True or False.

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 False otherwise.

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:

Yay!

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 True.

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:

yes
yes

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 False otherwise.

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

Comparison operators are applied to comparable objects and they return a Boolean value (True or False).

OperatorNameDescriptionExample
>Greater ThanReturns True if the left operand is greater than the right operand3 > 2 == True
<Less ThanReturns True if the left operand is smaller than the right operand3 < 2 == False
==Equal ToReturns True if the left operand is the same as the right operand(3 == 2) == False
!=Not Equal ToReturns True if the left operand is not the same as the right operand(3 != 2) == True
>=Greater Than or Equal ToReturns True if the left operand is greater than or equal to the right operand(3 >= 3) == True
<=Less Than or Equal ToReturns True if the left operand is less than or equal to the right operand(3 <= 2) == False