What's the difference between is and == in Python? #386
-
Hi everyone! I'm trying to understand the difference between I saw this code: a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True
print(a is b) # False
Why does == return True but is returns False?
Can someone explain this in simple terms? 🙏 |
Beta Was this translation helpful? Give feedback.
Answered by
MatteoMgr2008
Jul 3, 2025
Replies: 2 comments 1 reply
-
See https://docs.python.org/3.11/reference/expressions.html#is-not. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Great question! This is a very common confusion in Python.
In your example: a = [1, 2, 3]
b = [1, 2, 3]
a == b → True because the contents of both lists are equal
a is b → False because they are two different objects in memory
You can prove it by checking their IDs:
print(id(a))
print(id(b))
They'll be different!
Now check this:
x = [1, 2]
y = x
print(x is y) # True → both point to the same object |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Istituto-freudinttheprodev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great question! This is a very common confusion in Python.
==
checks if the values are equalis
checks if both variables point to the exact same object in memoryIn your example: