💬 Question: What is the difference between Python’s
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
== operators. But we’ll give a short intro at the end of this article too!
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
== operators, you can watch our two video tutorials on both operators to keep improving your understanding of Python basics! 👇
Python “is” Operator
is keyword tests if the left and right operands refer to the same object—in which case 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
The Python equal to (
left==right) operator returns
True when its
left operand is equal to its
right operand. Otherwise, it returns
3==3 evaluates to
3==2 evaluates to
🌍 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
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
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')
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
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!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.