Python Not Equal Operator

Python not equal operator

To check if a value is not equal to another, use Python not equal operator !=.

For example:

1 != 2

How Does the Not Equal Operator Work in Python

Python has six comparison operators. Here is a list of them:

>Less than
<Greater than
>=Less than or equal
<=Greater than or equal
!=Not equal

In this guide, we are interested in the not equal operator !=.

The not equal operator != checks if two values are not equal. It returns True if the values are not equal and False if they are.

Here are some example comparisons between different types of objects in Python:

1 != 2              # True
1 != 1              # False

[1, 2] != [2, 1]    # True
(1, 2) != (2, 1)    # True  

"Test" != "Banana"  # True

Now you have a basic understanding of how the not equal comparison operator works.

Let’s take a look at a slightly more advanced look at implementing the not equal behavior in a class.

If you are unfamiliar with Python classes, please skip this part.

Override the Not Equal Method __ne__() in a Class

When you call not equal on two objects, you are invoking the built-in __ne__() method under the hood. This method defines what happens when the objects are not equal.

For example, these two calls are equivalent to one another:

1 != 2.       # True
(1).__ne__(2) # True

In Python, you can override the comparison methods of your custom classes. This makes it possible to compare objects in a customized way using Python’s comparison operators.

For example, if you have a Fruit class, you can decide what happens when you call fruit1 != fruit2.

To customize the way comparison happens, override one (usually all) of the following special methods in your class:

__ne__Not equal
__lt__Less than
__gt__Greater than
__le__Less than or equal
__ge__Greater than or equal

As you came for the not equal operator, let’s see how to customize it in a class.

Here is an example of a Fruit class that has a customized way to compare if two objects are not equal by comparing the weights of the fruits. Follow the comments to keep up with the code:

class Fruit:
    # Initialize a fruit with name and weight
    def __init__(self, name, weight): = name
        self.weight = weight

    # When two fruit objects are compared with !=, this method is called on them
    def __ne__(self, other):
        return self.weight != other.weight

# Let's create two fruits with the same mass
banana = Fruit("Banana", 0.2)
pineapple = Fruit("Pineapple", 0.2)

# Even though the fruits are different, their masses are the same.
# As we customized the != operator by implementing the __ne__() method,
# this returns False
print(banana != pineapple)

# Under the hood, calling "banana != pineapple" is the same as calling:

# NOTICE that implementing the __ne__() method alone is meaningless.
# Instead, implement all the comparison methods to make it possible to compare your objects
# These are __eq__, __ne__, __lt__, __gt__, __le__, __ge__

Even though this example is pretty useless, it demonstrates how you can customize the not equal operator (and other operators) on your class.

To learn more about this so-called operator overloading, check out this article.

The Old-School Not Equal Operator ‘<>’ in Python

In Python 2 there is another operator for checking if two elements are not equal. The operator is <>.

Example use:

1 <> 2 # Returns True
1 <> 1 # Returns False

But do not use this. In Python 3 this operator is removed and it throws a syntax error.


Today you learned how to use the not equal operator in Python to check if a value is not equal to another.

1 != 2

You also learned how to support not equal operators in your custom class.

Thanks for reading. I hope you find it useful

Happy coding!

Further Reading

Python Interview Questions and Answers

Useful Advanced Features of Python

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest

Leave a Comment

Your email address will not be published.