# Python “==” versus “is”

Back to basics! If you are new to Python, you have likely seen these two equality comparison operators and may be unsure of their distinctly different capabilities. The question of Python “==” versus “is” comes down to use cases. I’m going to offer up a few examples to help.

Consider identical twins: they appear to be identical. But they are two separate and very different beings, yet their appearances are equal. Perhaps so similarly that you and others have a difficult time telling them apart.

ASK THESE QUESTIONS BEFORE CHOOSING YOUR OPERATOR:

Do they appear to be identical/the same?

Are they in fact the same person/object?

The “==” operator checks for equality between objects.

`>>> Twin == Twin`

True

The “is” operator compares identities.

`>>> Twin1 is Twin2`

False

Let’s play with some code and get a stronger grip on these two and their different functionalities. We will create a new list object and name it unusual_primes. Then define a variable indivisible_nums that points to that list object.

`>>> unusual_primes = [613, 6089, 81457]`

>>> indivisible_nums = unusual_primes

Let’s check to insure these two variables are set to point at seemingly identical lists.

`>>> unusual_primes`

[613, 6089, 81457]

>>> indivisible_nums

[613, 6089, 81457]

If we seek a boolean by comparing them for equality with the “==” operator, we get the expected result. They appear to be identical.

`>>> unusual_primes == indivisible_nums`

True

Are unusual_primes and indivisible_nums in fact pointing to the very same object (putting aside the fact that we know they do because we assigned them)? If we modify one, will we also then modify the other? Whether they are in fact pointing towards a single list object can be determined with the “is” operator.

`>>> unusual_primes is indivisible_nums`

True

Let’s get a little crazy in here and create an identical copy of this list object. We will use the list constructor to create a new list based on the passed sequence and name it prime_numbers:

`>>> prime_numbers = list(unusual_primes)`

Our new list looks identical to the list object pointed to by unusual_primes and indivisible_nums.

`>>> prime_numbers`

[613, 6089, 81457]

Now this is where the magic begins: compare the new prime_numbers list object to unusual_primes using the “==” operator. Are these two separate twins or merely a mirrored reflection?

`>>> unusual_primes == prime_numbers`

True

Equal in the eyes of Python! They appear to be identical — whether or not it is a reflection or two serparate persons. We have affirmed that prime_numbers and unusual_primes have the same contents. But are these two pointers identical and separate? Harness the “is” operator for the big reveal:

`>>> unusual_primes is prime_numbers`

False

Hmm! Python has set the lists prime_numbers and unusual_primes pointing to two different objects, regardless of identical content. Identical but different objects.

### “==” versus “is”

- If two variables are equal in content, the “==” expression evaluates to True.
- If two variables point to the same identical object, the Pythonic “is” expression evaluates to True.

Should you need to consider the use of “==” versus “is” — remember the twins!