Python not equal operator

By | September 27, 2021
. Leave empty if the image is purely decorative.

The “not equal to” operator is a comparison operator, and it returns a boolean value ( True or False) based on the comparison. The operator returns True if both the operands are of different data types or values, else it returns False.

Python is a dynamic and strong typed language which mean it compares the variable data type as well its value when it comes to “equal to” and “not equal to” operators.

Not Equal To syntax

In python 3 we represent the “not equal to” operator by != symbol.


>>> 20 != 30

>>> 20 != "20"

>>> 20 != 20

The != operator only operate between two operands or variables.


Python “not equal to” custom object

When we use the “not equal to” operator between two variables or objects, then at the backend the __ne__(self, other) method operates. This means we can use this Dunder method to create such user objects which could use python “not equal to” != operator.


class Student:
    def __init__(self, name, lan): name
        self.lan = lan

    # Return True if two Student objects has different languages
    # If both student has same languages return False
    def __ne__(self,other):
        if self. lan != other.lan:
            return True
        return False

rohit = Student("rohit", "Python")
himanshu = Student("himanshu", "Java")
raj = Student("raj", "Python")

print("rohit != himanshu", rohit !=himanshu)
print("rohit != raj", rohit!=raj)


rohit != himanshu True
rohit != raj False


  • In python, the not equal operator is represented by != symbol.
  • The operator can operate between two objects or variables.
  • It returns True if both the variable has different values or data types.
  • It returns False if both the variables have similar values and data type.
  • For user-defined functions, it can be customise using __ne__() dunder.

People are also reading: 

Leave a Reply

Your email address will not be published.