What is the difference between “==” and “is” variable in Python?
If you are new to python you must have seen programmers using “==” and “is”, sometimes even interchangeably. Prima-facie, they look similar and many times you will use any of them. But there is a big difference between them. I remember banging my head to the wall once, just because I had used “is” instead of “==” (thinking that both are same). So, in this post, we will see what is the exact difference between “==” and “is” variable and where and how they should be used?
”==” (Equality)
”==” or equality checks whether two variables have equal value or not?
“is” variable
The “is” variable not only check the values but also the identity of the variable. Hence, it checks whether the variable are identical i.e. they are the same object in the memory and have same object id.
Explaining it using a real-world example
Let us assume that I am at a donut store with my brother and we both are having donuts. I am having a chocolate donut and my brother is having a caramel donut.
'chocolate donut' == 'caramel donut'
# This will result in False
Now assume, that we both are having chocolate donuts, in that case:-
'chocolate donut' == 'chocolate donut'
# This will be true
Since, we are both having a chocolate donut, if we apply the equality check, it will probably leads to True.
However, if we run the “is” check, it should be false. Because I and my brother are having different donuts (though they are from same store and have same ingredients etc.) But if I put some syrup on my donut, it would not change my brother’s donut.
But, if I and my brother would have been sharing the same donut. Then, we might call that donut by different name - ‘my donut’, ‘my brother’s donut’ but that would have been the same object and putting syrup on my donut would have added syrup to my brother’s donut too. So, this is how “is” variable works.
Explaining it using coding example
Let us create two lists with different items and check using the equality variable (“==”)
list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 5]
if list_one == list_two:
print(True)
else:
print(False)
# Output
False
However, if we create two different lists (list_one and list_two) with similar items and check their equality:-
list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 3, 4, 5]
if list_one == list_two:
print(True)
else:
print(False)
# Output
True
However, if we will check these two list with “is” variable, the result will be false:-
list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 3, 4, 5]
if list_one is list_two:
print(True)
else:
print(False)
# Output
False
This is because, though both the lists have exact similar items but they are different lists/objects in the memory. The same can be checked by printing the ids of both the list. You will find out that they are different.
list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 3, 4, 5]
print(id(list_one))
print(id(list_two))
# Output
4388523912
4388523976
Now, let us create list_one and then assign variable list_two to it.
list_one = [1, 2, 3, 4, 5]
list_two = list_one
if list_one is list_two:
print(True)
else:
print(False)
# Output
True
In this case, both the lists are pointing to same memory location.
list_one = [1, 2, 3, 4, 5]
list_two = list_one
print(id(list_one))
print(id(list_two))
print(id(list_one) == id(list_two))
# Output
4455751560
4455751560
True
Now, both the lists point to the same object and since the lists are mutable objects, changing one will change the other two.
list_one = [1, 2, 3, 4, 5]
list_two = list_one
list_one[0] = 6
print(list_one)
print(list_two)
# Output
[6, 2, 3, 4, 5]
[6, 2, 3, 4, 5]
I hope this post explains the difference between “==” and “is” variable in Python. If you have any question, feel free to leave a comment. If you like this post, please share it with your friends.