Python is keyword is used to check if the memory reference of two Python objects are same or not.
Python is operator takes two operands and returns True if both the objects have same memory reference and False if not.
Example 1: Python IS Operators
In the following example, we will demonstrate the usage and functioning of Python is operator.
Run this program
a = [5, 8] b = [5, 8] c = a if a is b: print('a is b') else: print('a is not b') if a is c: print('a is c') else: print('a is not c')
a is not b a is c
In the above example, the contents of a and b are same, but a and b are different objects having a different memory allocation.
But when a is assigned to c, only the reference is copied to c, but not the entire contents. So, if you change the contents of c, then the contents of a also would be changed, because they refer to the same list in memory. Hence a is c returned True while a is b returned False.
In this tutorial of Python Examples, we learned how to use is keyword in Python, with the help of example programs.