SiteMap  | About    

'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:


In the example above, we can see they point to identical looking lists. Then we check the equality of these lists.

The above code output:

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

The above code output:

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 . (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.