Sunday, November 17, 2024
Google search engine
HomeLanguagesDifference between != and is not operator in Python

Difference between != and is not operator in Python

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 = 10
b = 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:

  1. First with integer data the output was false because both the variables a, b are referring to same data 10.
  2. Second with string data the output was false because both the variables c, d are referring to same data “Python”.
  3. 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 = 10
b = 10
print(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 list
list1 = []
list2 = []
list3 = list1
  
#First if
if (list1 != list2):
    print(" First if Condition True")
else:
    print("First else Condition False")
      
#Second if
if (list1 is not list2):
    print("Second if Condition True")
else:
    print("Second else Condition  False")
      
#Third if
if (list1 is not list3):
    print("Third if Condition True")
else
    print("Third else Condition False")
  
list3 = list3 + list2
  
#Fourth if
if (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:

  1. The output of the first if the condition is “False” as both list1 and list2 are empty lists.
  2. 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.
  3. The output of the third if the condition is “False” as both list1 and list3 are pointing to the same object.
  4. The output of the fourth if the condition is “True” because the concatenation of two lists always produces a new list.
RELATED ARTICLES

Most Popular

Recent Comments