Python NOT EQUAL operator
Last Updated :
14 Dec, 2023
In this article, we are going to see != (Not equal) operators. In Python, != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal.
Python NOT EQUAL operators Syntax
The Operator not equal in the Python description:
- != Not Equal operator, works in both Python 2 and Python 3.
- <> Not equal operator in Python 2, deprecated in Python 3.
Syntax: Value A != Value B
Return Type:
- Returns either True or False
Note: It is important to keep in mind that this comparison operator will return True if the values are the same but are of different data types.
Examples of NOT EQUAL Operator in Python
Here are a few examples of Python NOT EQUAL operators.
Example 1: NOT EQUAL Operator with same DataType
In this example, we are comparing different values of the same datatype, that is integers to see how all values are does not equal Python and how the NOT EQUAL operator works.
Python3
A = 1
B = 2
C = 2
print (A! = B)
print (B! = C)
|
Output:
True
False
Example 2: NOT EQUAL operator with different DataTypes
In this example, we are comparing similar values of the different datatypes to see how the NOT EQUAL operator works. We are taking an integer, a float, and a Python String as input.
Python3
A = 1
B = 1.0
C = "1"
print (A! = B)
print (B! = C)
print (A! = C)
|
Output:
False
True
True
Compare lists in Python using the Not Equal Operator
Python NOT EQUAL operator can also be used to compare two lists. Let’s see how can this be done.
In this example, we are taking 3 Python lists, out of which two are integers and one is a string list. Then we compared them using the does not equal operator in Python.
Python3
list1 = [ 10 , 20 , 30 ]
list2 = [ 10 , 20 , 30 ]
list3 = [ "geeks" , "for" , "geeks" ]
print (list1 ! = list2)
print (list1 ! = list3)
|
Output:
False
True
Use of if statement with the Not Equal operator in Python
The NOT EQUAL operator can also be used with the Python if else statements. Let us see a simple example of this.
In this example, we are comparing two strings and then printing a message based on the output of does not equal operator in Python.
Python3
str1 = 'Geeks'
str2 = 'GeeksforGeeks'
if str1 ! = str2:
print ( "Strings are not Equal" )
else :
print ( "Strings are Equal" )
|
Output:
Numbers are not Equal
Python NOT EQUAL Operator with Custom Object
We can also use the NOT EQUAL operator with custom objects in Python. Here is an example of how the does not equal Python operator works with custom objects.
The Python __ne__() decorator gets called whenever the does not equal Python operator in Python is used. We can override this function to alter the nature of the ‘not equal’ operator.
Python3
class Student:
def __init__( self , name):
self .student_name = name
def __ne__( self , x):
if type (x) ! = type ( self ):
return True
if self .student_name ! = x.student_name:
return True
else :
return False
s1 = Student( "Shyam" )
s2 = Student( "Raju" )
s3 = Student( "babu rao" )
print (s1 ! = s2)
print (s2 ! = s3)
|
Output:
True
True