Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python Not Equal operator

A comparison operator identifies the relationship between the operands and results into True or False.

One such basic yet important operator is not equal operator in Python.

It returns True if the values on either side of the operator are unequal i.e. it returns True when two variables of same type have different values, else it returns False.

OperatorDescription
!=Not Equal operator, applicable and available in both Python 2 and Python 3.
<>Not equal operator in Python 2, deprecated in Python version 3 and above.

Python not equal Operator Syntax

operand1 != operand2

Example 1:

In the below snippet, 10 <> 20 results in a syntax error because it is deprecated in Python version 3 and above.

Python Not Equal Operator
Python Not Equal Operator

Example 2:

Python Not Equal Operator
Python Not Equal Operator

Output:

Output Not Equal Operator
Output Not Equal Operator

Python not equal Operator with custom object

Whenever we use the not equal operator, it calls __ne__(self, other) function. Thus, the user can define their own custom implementation for the objects and manipulate the natural or default outcome/output.

Not Equal Custom Object
Not Equal Custom Object

Output:

Output Not Equal Custom Object
Output Not Equal Custom Object

References