'is' and '==' operators in Python

The is operator compares the identity of two objects while the == operator compares the values of two objects. There is a difference in meaning between equal and identical. And this difference is important when you want to understand how Python's is and == comparison operators behave. The == operator is used when the values of two operands are equal, then the condition becomes true. The is operator evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

Consider the following example:

list_1 = ['a', 'b', 'c'] list_2 = list_1 list_3 = list(list_1) print(list_1) print(list_2) print(list_3)
['a', 'b', 'c'] ['a', 'b', 'c'] ['a', 'b', 'c']
In the example above, we can see they point to identical looking lists. Then we check the equality of these lists.
print(list_1 == list_2) print(list_1 == list_3)

The above code output:

True True
This is because their values of list_1, list_2, list_3 are equal , then the condition becomes true.
print(list_1 is list_2) print(list_1 is list_3)

The above code output:

True False
Here you can see (list_1 is list_3) is False because list_1 and list_3 are pointing to two different objects , even though their contents might be the same. So, we can say "is" will return True if two variables point to the same object and "==" if the objects referred to by the variables are equal .