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 = list1
if (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, and list4.
  • Line 6: We check if list1 and list2 point to the same object. Here, both point to different objects. So, the is method returns false.
  • Line 11: We check if list2 and list3 have the same values. Here, they have the same values. So, the == method returns true.
  • Line 16: We check if list1 and list2 have the same values. Here, they have different values. So, the == method returns false. (They point to the same object, but have different values.)
  • Line 22: We check if list2 and list3 point to the same object. Here, both point to different objects. So, the is method returns false. (They have the same values, but point to different objects.)
  • Line 27: We check if list1 and list4 point to the same object. Here, both point to the same object. So, the is method returns true.
  • Line 32: We check if list1 and list4 have the same values. Here, they have the same values. So, the == method returns true.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved