Understanding ‘==’ vs. ‘is’ in Python

Explaining which to use when

Image for post
Image for post
Photo by on

All of us have come across a situation while solving some problems in Python when we have to compare two objects. For this, many of us have used either the == or is operator.

But, it seems many of us are confused about which one to use in which situation.


Analyzing the Difference

In this post, I will be analyzing the basic difference between them.

For this, we will start with some analogy with twin cats. So, assume that you have twin cats which are very similar. They have the same charcoal fur and the same piercing green eyes. Just by looking, we can’t tell which one is which.

Now, come to our == vs is operators in Python.

The == operator compares by checking for the equality. If these two cats were two Python objects, then if comparing them using the == operator, it will return ‘Both cats are Equal’ as the answer.

The is operator compares by checking for the identity. If we use is for comparing them we will get ‘Both cats are not Equal’ as the answer.

To really understand this, we will write some code.

First, we will create a list named a which contains elements [3, 4, 5] and another list b which will point to the list a.

Image for post
Image for post

Let's inspect these two lists. We can see that these looks point to the identical-looking list.

Image for post
Image for post

Because the two list objects look the same, we’ll get the expected result
when we compare them for equality by using the == operator.

Image for post
Image for post

However, that doesn’t tell us whether a and b are actually pointing to
the same object. Of course, we know they are because we assigned
them earlier, but suppose we didn’t know — how might we find out?

The answer is to compare both variables with the is operator. This
confirms that both variables are, in fact, pointing to one list object.

Image for post
Image for post

Now, let’s create another list c using the list() which contains the elements of the list a.

Image for post
Image for post

If we look at list c, it looks similar to the list pointed to by a and b.

Image for post
Image for post

Now, we will compare a and c using the == and is operators.

Image for post
Image for post

Here, the == operator gives True because both of them have the same content.

Image for post
Image for post

Python is telling us that c and a are pointing to two different objects, even though their contents might be the same.

So, to recap, let’s try and break down the difference between is and
== into two short definitions.

  1. An is expression evaluates to True if two variables point to the
    same (identical) object.
  2. A == expression evaluates to True if the objects referred to by
    the variables are equal (have the same contents).

So, I think this post will clear your doubts about == and is.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store