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.
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")
list1
, list2
, list3
, and list4
.list1
and list2
point to the same object. Here, both point to different objects. So, the is
method returns false
.list2
and list3
have the same values. Here, they have the same values. So, the ==
method returns true
.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.)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.)list1
and list4
point to the same object. Here, both point to the same object. So, the is
method returns true
.list1
and list4
have the same values. Here, they have the same values. So, the ==
method returns true
.Free Resources