Python-not equal operator and _ne_() magic method

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in python. One of the 7 classifications is the comparison operators.

Just as the name suggests, these operators are used to compare the values of two operands:

  1. Greater than >
  2. Less than <
  3. Greater than or equal to >=
  4. Less than or equal to <=
  5. Equals ==

These five are not new, we use them in mathematics. The one new operator is the "not equal to" operator. It is the opposite of equal to operator. How interesting is that?

This tutorial discusses the "not equal to" operator with examples and explains how we can customize the behavior of the function using the magic method __ne__() called the dunder method in a class.

Operator: ! =

Name: Not equal to

Type: Comparison operator

Syntax: value 1! = value 2

Return Type: Boolean

Return value:  If value1 and value2 on either side of the operator are not equal, True will be returned; else, False will be returned if both the values are equal.

Example programs:

#1:

a = int (input ("Enter the first number: "))
b = int (input ("Enter a number to compare: "))
print (a != b)

Output:

Case 1:

Enter the first number: 3

Enter a number to compare: 3

False

Case 2:

Enter the first number: 4

Enter a number to compare: 8

True

Understanding:

The program takes input for two numbers to use the operator. If the user gives the same input for the 2 numbers, the operator returns False, and when different inputs are given, it returns true.

#2:

As a condition:

a = int (input ("Enter the first number: "))

b = int (input ("Enter a number to compare: "))

res = a != b

if (res == True):

    print ("The values are not equal")

else:

    print ("The values are not equal")

Output:

Case 1:

Enter the first number: 3

Enter a number to compare: 2

The values are not equal

Case 2:

Enter the first number: 3

Enter a number to compare: 3

The values are equal

Understanding:

In the above program, we compared the two numbers, stored the result in res, and used it as a condition in the if statement to print if the two values are equal.

#3 Data type variation

a = int (input ("Enter the first number: "))

b = input ("Enter a number to compare: ")

res = a != b

if (res == True):

    print ("The values are not equal")

else:

    print ("The values are not equal")

Output:

Enter the first number: 4

Enter a number to compare: 4

The values are not equal

Understanding:

The input is the same for both values. But why is it false? It is because, when taking the input for the second number, int is not mentioned. Hence, the first number is an integer, and the second number is a string. An integer is not equal to a string.

  • Even if we take the same number, if both the numbers are of different data types, they are not considered equal.

Precedence and Associativity:

If multiple comparison operators are used, there is no proper rule for precedence and associativity. Unless the expression is well-defined with parenthesis, it is evaluated from left to right by breaking it down into pieces.

If we say 2!= 3!= 4

We get true: It compares if the three values are not equal to each other.

But if we say:

X < Y < Z

This expression neither means (X < Y) < Z nor X < (Y < Z). It just means X < Y and Y < Z. By default, it will be evaluated from left to right.

OOPS: In a class:

__ne__()-The magic method also called the dunder method:

Python internally calls this magic method whenever we compare two objects using the not equal operator, it automatically gives the result pertaining to the original != functionality where it checks whether both the objects are occupying different memory locations. Using this method, we can customize our operator behavior by overriding the body of the function.

Syntax:

__ne__(self, other)

ne: not equal

class not_eq :

    def __init__ (self, num):

        self. num = num

    def __ne__ (self, a):

        if (type (a.num) != type (self.num)):

            return True

        if self. num != a. num:

            return True

        else:

            return False

obj1 = not_eq (int (input ("Enter number 1: ")))

obj2 = not_eq (int (input ("Enter number 2: ")))

print (obj1 != obj2)

Output:

Enter number 1: 3

Enter number 2: 2

True

Understanding:

In the program, we created a class, “not_eq," in which we wrote our magic method __ne__ () -> the predefined function that gets invoked by default when we call the! = operator anywhere in the program. We created two objects with values taking inputs from the user and using the!= operator. By overloading the operator, we can now compare two objects.

Example:

Class age:

    def __init__ (self,age):

        self.age = age

    def __ne__ (self, a):

        if self.age != a.age:

            return True

        else:

            return False

Person1 = age(int (input ("Enter the age of participant 1: ")))

Person2 = age(int (input ("Enter the age of participant 2: ")))

print (Person1 != Person2)

Output:

Enter number 1: 33

Enter number 2: 23

True