## DEV Community π©βπ»π¨βπ» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Posted on

# Object Comparisons: βisβ vs β==β

The == operator compares by checking for equality: if these Python objects we compared them with the == operator, weβd get both objects are equal as an answer.

The is operator, however, compares identities: if we compared our
objects with the is operator, weβd get these are two different objects as an answer.

letβs take a look at some real Python code.

``````>>> a = [1, 2, 3]
>>> b = a
``````

Letβs inspect these two variables. We can see that they point to
identical-looking lists:

``````>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
``````

Because the two list objects look the same, weβll get the expected result
when we compare them for equality by using the == operator:

``````>>> a == b
True
``````

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:

``````>>> a is b
True
``````

Letβs see what happens when we create an identical copy of our list
object. We can do that by calling list() on the existing list to create
a copy weβll name c:

``````>>> c = list(a)
``````

Again youβll see that the new list we just created looks identical to the
list object pointed to by a and b:

``````>>> c
[1, 2, 3]
``````

Now this is where it gets interesting. Letβs compare our list copy c with
the initial list a using the == operator. What answer do you expect to
see?

``````>>> a == c
True
``````

Okay, I hope this was what you expected. What this result tells us
is that c and a have the same contents. Theyβre considered equal by
Python. But are they actually pointing to the same object? Letβs find
out with the is operator:

``````>>> a is c
False
``````

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

## Conclusion:

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