Difference between == and is operator

Difference between == and is operator

== The operator compares the values ​​of the operands and checks for value equality. The operator checks whether the two operations represent the same object.



Example :

list1 = []
list2 = []
list3=list1
  
if (list1 == list2):
    print("True")
else:
    print("False")
  
if (list1 is list2):
    print("True")
else:
    print("False")
  
if (list1 is list3):
    print("True")
else:    
    print("False")
  
list3 = list3 + list2
  
if (list1 is list3):
    print("True")
else:    
    print("False")

Output:

True
False
True
False

  • The first "output" is correct if the first list 1 and list 2 are both blank lists.
  • The second is if the condition shows "false" because the two blank lists are in different memory locations. Hence Table 1 and Table 2 represent different objects. We can see this in Python with the ID () function that returns the "identity" of an object.
  • If the output of the third position is "True", then Table 1 and Table 3 both refer to the same object.
  • If the situation is "false", the fourth output is because the combination of the two lists always produces a new list.

Example :

list1 = []
list2 = []
  
print(id(list1))
print(id(list2))

Output:

2375830558792

2375830927816






In this article i have explained the difference between == and is operator

















Post a Comment

0 Comments