What is the difference between == and is in Python?
In Python, the == operator checks if the given operands have equal values or not.
Meanwhile, the is operator determines if the given operands point to the same object or not.
Example
list1 = [1]list2 = []list3 = []list4 = list1if (list1 is list2):print("True")else:print("False")if (list2 == list3):print("True")else:print("False")if (list1 == list2):print("True")else:print("False")if (list2 is list3):print("True")else:print("False")if (list1 is list4):print("True")else:print("False")if (list1 == list4):print("True")else:print("False")
Explanation
- Lines 1 – 4: We declare 4 different lists:
list1,list2,list3, andlist4. - Line 6: We check if
list1andlist2point to the same object. Here, both point to different objects. So, theismethod returnsfalse. - Line 11: We check if
list2andlist3have the same values. Here, they have the same values. So, the==method returnstrue. - Line 16: We check if
list1andlist2have the same values. Here, they have different values. So, the==method returnsfalse. (They point to the same object, but have different values.) - Line 22: We check if
list2andlist3point to the same object. Here, both point to different objects. So, theismethod returnsfalse. (They have the same values, but point to different objects.) - Line 27: We check if
list1andlist4point to the same object. Here, both point to the same object. So, theismethod returnstrue. - Line 32: We check if
list1andlist4have the same values. Here, they have the same values. So, the==method returnstrue.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved