The double equals ==
operator and the is
keyword are commonly used statements for comparing objects in Python. In this article, you’ll learn the difference between these two statements with the help of several examples.
The Double Equals ==
Operator
The double equals ==
operator returns True
when the values assigned to the objects being compared are equal. The ==
operator doesn’t match the memory locations of two objects while comparing the object so, even if two objects have different memory locations but the same values, the double equals operator will return True
. In short, the ==
operator compares objects for equality. Let’s see an example.
The script below defines three lists
car1 = ["Honda", "Toyota", "Ford"]
car2 = ["Ford", "Toyota"]
car3 = ["Honda", "Toyota", "Ford"]
Now, we’re going to use the ==
operator to compare lists False
.
if car1 == car2:
print("values are equal")
else:
print("values are not equal")
Output:
values are not equal
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.
Let’s see another example where we’ll compare the lists
if car1 == car3:
print("values are equal")
else:
print("values are not equal")
Since the two lists in this comparison have exactly the same number and type of items, the ==
operator returns True
.
Output:
values are equal
The is
keyword
The is
keyword compares identities of two or more objects by matching their memory locations. Even if two objects contain same items, the is
keyword will return False
if the objects do not point to the same memory location.
Let’s see an example. The following script compares the is
keyword. Recall that these two lists contain the same items.
if car1 is car3:
print("values are equal")
else:
print("values are not equal")
Output:
values are not equal
The output above shows that the comparison returns False
despite the False
? It’s because the memory locations of lists
You can check the memory location of an object by passing the object to the id()
method. The following script prints the memory locations of our
print(id(car1))
print(id(car3))
The output below shows that memory locations for our list objects are indeed different.
Output:
1609071352384
1609071351936
Let’s assign the
car3 = car1
Let’s again check the memory locations of our two lists.
print(id(car1))
print(id(car3))
Output:
1609071352384
1609071352384
From the above output you can see that now both the
Now that we’ve done that, let’s compare the is
keyword.
if car1 is car3:
print("values are equal")
else:
print("values are not equal")
Since the comparison returns True
, you’ll now see the following output:
Output:
values are equal
Conclusion
To conclude, the double equals ==
operator matches equality and compares two objects for their values, irrespective of their memory locations. On the other hand, the is
keyword matches identity of objects by comparing their memory locations.
Get Our Python Developer Kit for Free
I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.