Python Not Equal Operator
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. These are the same operators we use in mathematics:
- Greater than - >
- Less than - <
- Greater than or equal to - >=
- Less than or equal to - <=
- Equals - ==
The one new operator is the "not equal to" operator. It is the opposite of equal to operator. How interesting is that? This article discusses the "not equal to" operator with examples.
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 both the 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 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:
Case 1:
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.
- 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 equal.
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.
Examples:
#Operator overloading
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 used the! = operator. By overloading the operator, we can now compare two objects.