What is the difference between the '==' and 'is' operator in Python?
Both operators are quite similar, we will explain when to use each of them.
Compare two objects is a pretty common task while coding any kind of software. To compare strings and numeric variables we usually apply the equality operator ==
, however, to compare two objects the operator is
is more suitable.
In this post we try to explain how these two operators work, showing some examples and trying to clarify your doubts on this subject.
Feel free to comment or get in touch with me if necessary; or if you have any suggestions for new posts/articles.
Equality or identity?
To better understand these two concepts we will use an analogy of two twins. They look the same. If you look at a picture of them, probably you couldn't say who is who.
The ==
operator is called equality. If the twins are Python objects, then if we use ==
operator, it will return "Both are Equal" as the answer.
The is
operator is called identity. If we use is
for comparing them we will get "Both are not Equal" as the answer. They have different names, tastes, and behavior.
Everyone knows they look the same, but in deep, they are not the some person, right? Many of them have the opposite personality of their brother/sister. Every person needs to develop their personality and life, right?
Now, let’s see some code to expand these concepts.
If It is not clear with this traditional example with twins, maybe with integers and lists you will get it.
Be careful with small integers (they are interned)
In our first example, we compare variables a and b. Both of them assigned 256 as value. And variables c and d, assigned with 257. Take a look at the figure below. Does the result make sense to you?
First of all, if you are interested in comparing integers values, you should use the equality operator == . The identity operator will perform a comparison of the memory address. Imagine they will work as id(a)==id(b), where id() function returns the memory address of each variable.
Second, behind the scenes, Python interns objects with commonly-used values (for example, the integers -5 to 256) to save memory.
Therefore, the result of comparisons between c and d is False when using the operator is.
If you are not feeling confident, try the code into your machine. In code we trust.
In code we trust.
The id() function returns the memory address of a variable. It is known as variable identity.
Comparing two lists
We will create a list named list_a
which contains elements [1, 2, 3]
, and another list list_b
which will be a copy of list_a.
Using printing we can see that both content variables seem to be identical. As they look the same, it's expected the result to be "True" when we compare using the ==
operator.
To compare if both variables are pointing to the same object we should compare than using the operatoris
. This command will confirm that both variables are pointing to one list object.
Using id()
function we can check if variables are pointing to the same address. Now, let’s create another list namedlist_c
using the list()
which contains the elements of the list a
.
If we look at list_c
, it looks similar to the list pointed to by list_a
and list_b
. However, It hasn't the same identity of list_a
as list_b
has.
We can verify that ==
operator gives True
because both of them have the same content. However, list_c
is stored in a different address, then the operator is
gives a “False” result.
Python is telling us that list_c
and list_a
are pointing to two different objects, even though their contents might be the same.
The Figure below is another example of when two lists can have the same content and two different identities. Take a look at the two last prints and observe how the variable is stored into two different addresses.
Finally, pay attention when you are copying variables. The way you copy them may create a pointer and when you modify one, you will modify the other.
Using the Right Operators
Now that we have understood the difference between the == and is let us review when we should use these two operators.
Using Equality ( == )
The standard recommendation is to use the == and != operators to compare values. Except when comparing to None. In this case, you are not concerned about the memory location of the variables. You only want to check if the content in both these variables is the same.
Using Identity ( is )
Use this operator if you want to compare the identity of two objects, that is if they are stored in the same memory location, use the is and is not operators. These operators are especially useful in comparing variables to None and are preferred over class methods while comparing variables to None. In other cases, avoid it.
So, I hope this post clarifies any doubts you have about ==
and is
. If instead you have more doubts now, leave a comment and I will answer you!