It’s really confusing for Python programmer. When to use ‘is’? When to use ‘==’?
And what is the Difference Between is and == in Python?
Before getting into the difference here is quick syntax, you should understand.
A syntax for “is” expression:
a is b
A Syntax for “==” (equals) expression:
a == b
Note: Where ‘a’ and ‘b’ are two different Python objects.
If we consider it as a native language, both expression seems to be doing the same work. But does not hold true for Python.
So to find the difference between them, let’s make it simple.
I will give you quick demonstration by using ”is” and “==” expressions with Python list.
Without getting heedless, let’s take the example of using “is” and “==” on the Python list.
[python]
a = [1, 2, 3]
b = a
a is b
#True
c = list(a)
a is c
#False
[/python]
So are you surprised by the output?
Then how “==” (equals) is different from “is”?
[python]
a = [1, 2, 3]
b = a
a == b
#True
c = list(a)
a == c
#True
[/python]
Related read to explore Python List:
This is all about the Difference Between is and == in Python. I know its little confusing. If you have any comment, feel free to write below.