We use the is operator when we want to compare two objects’ identities. On the other hand, we use the == operator when we want to compare the two objects’ values. The meaning of “identical” and “equal” is different, and this difference comes to play when we try to understand the usage and behaviour of the == operator in Python. In this article, we will discuss the difference between == and ‘is’ operators in Python. Read ahead to learn more.
What is == Operator?
We use the == operator in Python when the values of both the operands are equal. Thus, the condition would become true here.
What is the ‘is’ Operator?
An is operator would evaluate to true whenever the variables present on either side of an operator would point towards the very same object. Otherwise, it would give us a false evaluation.
Let us take a look at an example to understand this better:
list_4 = [‘m’, ‘n’, ‘o’]
list_5 = list_4
list_6 = list(list_4)
print(list_4)
print(list_5)
print(list_6)
The output here would be:
[‘m’, ‘n’, ‘o’] [‘m’, ‘n’, ‘o’] [‘m’, ‘n’, ‘o’]In the example given above, you can see that these point towards lists that look identical. After this, we will check if these lists are equal. There is a difference between equal and identical here.
print(list_4 == list_5)
print(list_4 == list_6)
The output of the code mentioned above would be:
True
True
It would happen because the values of list_4, list_5 and list_6 are equal, and thus, the condition would become true.
Now consider this code:
print(list_4 is list_5)
print(list_4 is list_6)
The output of the code mentioned above would be:
True
False
You can see here that the code (list_4 is list_6) is False, since list_4 and list_6 are pointing towards two separate objects even if they might have the same contents. Thus, we can conclude that “is” would return to be True when both the variables point towards the very same object and “==” would return to be True when the objects that are referred to by the available variables are equal.
Difference between == and is operators in Python
Let us talk about the differences between == and ‘is’ operators in Python.
Parameters | == Operator | is Operator |
Name | The ‘==’ is known as the equality operator. | The ‘is’ is known as the identity operator. |
Basics | The == operator helps us compare the equality of objects. | The is operator helps us check whether different variables point towards a similar object in the memory. |
Uses | We use the == operator in Python when the values of both the operands are very much equal. Thus, the condition would become true here. | An is operator would evaluate to true whenever the variables present on either side of an operator would point towards the very same object. Otherwise, it would give us a false evaluation. |
Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Application Form, GATE Syllabus, GATE Cut off, GATE Previous Year Question Paper, and more.
Also Explore,
Comments