Home » Python Tutorial » Python Not Equal Operator with Examples

Python Not Equal Operator with Examples

As we all know, there are many operators predefined in Python like logical operators, arithmetic operators, binary operators, identity operators, etc. We are discussing equality operators. The equality operators are used to check the equality between two data sets. The Python not equal operator returns true if the two data sets are equal or return false if there is no equality in both of them. There are three types of equality operators in Python.

  1. == (equal equal)
  2. != (not equal)
  3. <> (deprecated)

However, The deprecated (<>) operator is removed from Python 3.

How to do not equal in Python?

The not equal operation is the most performed task in the if-else statement. To check the equality between two variables, the equality operators (== and !=) are used. To check disequality between the variables, use Python not equal operator. Let’s learn this with the help of an example

Example:

a = int(input("Enter first number:"))
b = int(input("Enter second number:"))

if(a!=b):
   print("Both numbers are not equal")
else:
   print("Both numbers are equal")

Output

Enter first number:5
Enter second number:4
Both numbers are not equal

In the above example, We are checking the equality between two numbers. If the two numbers are equal, then it will display Both numbers are equal. Or it will display Both numbers are not equal.

Now, let’s check the equal operator returns if both the numbers are equal or vice versa.

Example:

a = int(input("Enter first number:"))
b = int(input("Enter second number:"))

print(a!=b) #if both are not equal
print(a==b) #if both are equal

Output

Enter first number:5
Enter second number:4
True
False

Applying Python not equal to checking the equality between characters and strings.

Checking the equality between characters and Strings is possible. The Python interpreter uses an Unicode system to check the equality between them. Let’s understand this with an example.

1. Checking equality between characters

Example:

a = input("Enter first character:")
b = input("Enter Second character:")
if(a!=b):
   print("Both characters are not equal")
else:
   print("Both characters are equal")

Output

Enter first character:a
Enter second character:b
Both characters are not equal

2. Checking equality between string

Example:

a = input("Enter first string:")
b = input("Enter second string:")
if(a!=b):
   print("Both strings are not equal")
else:
   print("Both strings are equal")

Output

Enter first string:alex
Enter second string:ben
Both strings are not equal

Checking the equality between two data structures is also possible in Python.

Checking equality between two lists using not equal operator.

Example:

n = int(input("Enter number of elements in list:"))
a = []
b = []
print("Enter elements of 1st list:")
for i in range(0,n):
   x = int(input("Enter element:"))
   a.append(x)

print("Enter elements of 2nd list:")
for i in range(0,n):
   y = int(input("Enter element:"))
   b.append(y)

if(a!=b):
   print("Both lists are not equal")
else:
   print("Both lists are equal")

Output

Enter number of elements in list:5
Enter elements of 1st list:
Enter element:1
Enter element:2
Enter element:3
Enter element:4
Enter element:5
Enter elements of 2nd list:
Enter element:6
Enter element:7
Enter element:8
Enter element:9
Enter element:0
Both lists are not equal
How to do not equal in python?

Checking if two variables are equal or not is very easy in python. The example is given below.
Example:
a = 10
b = 15
print(a!=b)


Output:
True
The above output shows that both numbers are not equal.

Did you know?
1. Python return
2. Python Strftime
3. Encapsulation in Python
4. Operator Overloading in Python
5. iloc in Python
6. Python Pop
7. Python Enum
8. Python Pass
9. Python deque
10. Python Not Equal Operator

Pin It on Pinterest