The Wayback Machine - https://web.archive.org/web/20211029225126/https://www.geeksforgeeks.org/difference-operator-python/amp/

Difference between == and is operator in Python

The Equality operator (==) compares the values of both the operands and checks for value equality. Whereas the is’ operator checks whether both the operands refer to the same object or not (present in the same memory location).
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# python3 code to
# illustrate the
# difference between
# == and is operator
# [] is an empty list
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




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

Output: 

139877155242696
139877155253640

This shows list1 and list2 refer to different objects.




Article Tags :