Difference between == and is operator in Python
The Equality operator (==) is a comparison operator in Python that compare values of both the operands and checks for value equality. Whereas the ‘is’ operator is the identity operator that checks whether both the operands refer to the same object or not (present in the same memory location).
# Equality operator >>> a=10 >>>b=10 >>>a==b True >>>a=10 >>>id(a) 2813000247664 >>>b=10 2813000247664 # Both a and b is pointing to the same object >>>a is b True >>>c=a # Here variable a is assigned to new variable c, which holds same object and same memory location >>> id(c) 2813000247664 >>>a is c True
True False True False
- The output of the first if the condition is “True” as both list1 and list2 are empty lists.
- Second, if the condition shows “False” because two empty lists are at different memory locations. Hence list1 and list2 refer to different objects. We can check it with id() function in python which returns the “identity” of an object.
- The output of the third if the condition is “True” as both list1 and list3 are pointing to the same object.
- The output of the fourth if the condition is “False” because the concatenation of two lists always produces a new list.
This shows that list1 and list2 refer to different objects.