“is” vs “==” Python Identity and Equality

πŸ’¬ Question: What is the difference between Python’s is and == operators?

Answer

The Python “==” operator compares equality of values whereas the Python “is” operator compares identity of objects, i.e., do the two operands point to the same object in memory.

For example, the expression [1, 2, 3] == [1, 2, 3] returns True because both lists contain the same elements, they are equal. But the expression [1, 2, 3] is [1, 2, 3] returns False because they are two different lists that point to different objects in memory, they are not identical.

>>> [1, 2, 3] == [1, 2, 3]
True
>>> [1, 2, 3] is [1, 2, 3]
False

However, the expression 123 == 123 returns True because both integers are the same. And the expression 123 is 123 will also return True because the values point to the same object in memory!

>>> 123 == 123
True
>>> 123 is 123
True

Feel free to dive into both tutorials to learn about the is and == operators. But we’ll give a short intro at the end of this article too!

Recommended Tutorials:

Before you learn about the important concept of mutable and immutable objects in Python, as well as about both of those operators, feel free to check out our memory visualizer tool first:

Try It Yourself Example – Memory Visualization “is” vs “==”

You can try our interactive memory visualization by clicking “Next” to see how identity and equality are two different concepts:

Here’s the code for copy&paste:

lst_1 = [1, 2, 3]
lst_2 = [1, 2, 3]

print(lst_1 == lst_2)
# True

print(lst_1 is lst_2)
# False

If you change one list, the other won’t be affected by the change. After changing the initially equal but non-identical list, they become non-equal and non-identical:

# Change one list:
lst_1.append(4)

print(lst_1)
# [1, 2, 3, 4]

print(lst_2)
# [1, 2, 3]

print(lst_1 == lst_2)
# False

Similarly, you can check out equality and identity for integers where it works differently:

Here’s the code for copy&paste:

val_1 = 123
val_2 = 123

# Equality:
print(val_1 == val_2)
# True

# Identity:
print(val_1 is val_2)
# True

Let’s quickly recap the is and == operators, you can watch our two video tutorials on both operators to keep improving your understanding of Python basics! πŸ‘‡

Python “is” Operator

The Python is keyword tests if the left and right operands refer to the same objectβ€”in which case it returns True.

It returns False if they are not the same object, even if the two objects are equal.

For example, the expression [1, 2, 3] is [1, 2, 3] returns False because although both lists are equal, they are two independent objects in memory.

🌍 Recommended Tutorial: Python is Operator β€” Checking Identity

Python “==” Operator

Image: Equal but not the same!

The Python equal to (left==right) operator returns True when its left operand is equal to its right operand. Otherwise, it returns False.

For example, 3==3 evaluates to True, but 3==2 evaluates to False.

🌍 Recommended Tutorial: Python Equal To Operator β€” Checking Equality

A Word on Mutability

Why are some variables compared using equality and others using identity?

The reason why two equal lists are considered equal but not necessarily identical and two equal numbers are considered equal but necessarily identical is that lists are mutable (they can be changed) and integers are immutable (they cannot be changed).

As you can change a list object, Python must create two list objects even if they contain the same elements. Why? Because you may change one list in your code and the change should not affect the other list.

Well — you cannot change objects such as integers or strings.

So, Python may decide to optimize for memory and let both variables point to the same object in memory. No variable pointing to that immutable object can ever change the object. Thus, there is no risk of aliasing where one variable is changed and the change is visible at the other variable too.

You can learn more about mutable vs immutable objects in the following video:

Recommended Finxter Tutorials:

When to Use Which “==” vs “is”?

Use the equality operator == when comparing whether a certain variable has obtained a certain value. In fact, you’ll use the == operator in the vast majority of cases in if conditions, while loops, or ternary operators. Each time you must know if a variable has obtained a certain value programmatically or by means of user input, you’ll use the == operator.

Use the identity operator is when comparing whether two variables refer to the same object. This is usually only done in debugging when you want to uncover some aliasing problems etc., i.e., whether the change of one variable unexpectedly results in a change of another variable. Outside of debugging, you’ll seldomly use the identity operator.

The most frequent use of the identity operator would be comparing a value to None, i.e., you would write

if x is None:
    print('hello')

and not

if x == None:
    print('hello')

The reason is that there is only one instance of the None type, the None object itself. All variables set to None would point to the same None object in memory. There cannot be two variables that point to different None objects.

Thanks for Reading this Tutorial ❀️

If you want to keep learning, feel free to download my cheat sheets—they are 100% free and super helpful, promised!