Difference between != and is not operator in Python - GeeksforGeeks

11 Dec, 2020
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. Whereas is not operator checks whether id() of two objects is same or not. If same, it returns False and if not same, it returns True. And is not operator returns True if operands on either side are not equal to each other, and returns false if they are equal.
Let us understand the concepts one by one:
Example 1:
Python3
a = 10b = 10  print(a is not b)print(id(a), id(b))  c = "Python"d = "Python"print(c is not d)print(id(c), id(d))  e = [1,2,3,4]f = [1,2,3,4]print(e is not f)print(id(e), id(f))
Output:
False
140733278626480 140733278626480
False
2693154698864 2693154698864
True
2693232342792 2693232342600
Explanation:
First with integer data the output was false because both the variables a, b are referring to same data 10.Second with string data the output was false because both the variables c, d are referring to same data “Python”.Third with list data the output was true because the variables e, f have different memory address.(Even though the both variable have the same data)
First with integer data the output was false because both the variables a, b are referring to same data 10.
Second with string data the output was false because both the variables c, d are referring to same data “Python”.
Third with list data the output was true because the variables e, f have different memory address.(Even though the both variable have the same data)
Example 2:
 != 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. 
Python3
# Python3 code to # illustrate the # difference between# != and is operator  a = 10b = 10print(a != b)print(id(a), id(b))  c = "Python"d = "Python"print(c != d)print(id(c), id(d))  e = [ 1, 2, 3, 4]f=[ 1, 2, 3, 4]print(e != f)print(id(e), id(f))
Output:
False
140733278626480 140733278626480
False
2693154698864 2693154698864
False
2693232369224 2693232341064
Example 3:
The != operator compares the value or equality of two objects, whereas the Python is not operator checks whether two variables point to the same object in memory.
Python3
# Python3 code to # illustrate the # difference between# != and is not operator# [] is an empty listlist1 = []list2 = []list3 = list1  #First ifif (list1 != list2):    print(" First if Condition True")else:    print("First else Condition False")      #Second ifif (list1 is not list2):    print("Second if Condition True")else:    print("Second else Condition  False")      #Third ifif (list1 is not list3):    print("Third if Condition True")else:     print("Third else Condition False")  list3 = list3 + list2  #Fourth ifif (list1 is not list3):    print("Fourth if Condition True")else:     print("Fourth else Condition False")
Output:
First else Condition False
Second if Condition True
Third else Condition False
Fourth if Condition True
Explanation:
The output of the first if the condition is “False” as both list1 and list2 are empty lists.Second if the condition shows “True” because two empty lists are at different memory locations. Hence list1 and list2 refer to different objects. We can check it with id() function in python which returns the “identity” of an object.The output of the third if the condition is “False” as both list1 and list3 are pointing to the same object.The output of the fourth if the condition is “True” because the concatenation of two lists always produces a new list.
The output of the first if the condition is “False” as both list1 and list2 are empty lists.
Second if the condition shows “True” because two empty lists are at different memory locations. Hence list1 and list2 refer to different objects. We can check it with id() function in python which returns the “identity” of an object.
The output of the third if the condition is “False” as both list1 and list3 are pointing to the same object.
The output of the fourth if the condition is “True” because the concatenation of two lists always produces a new list.
Python-Operators
Difference Between
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between Process and Thread
Difference Between Spark DataFrame and Pandas DataFrame
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Clustered and Non-clustered index
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe