Introduction
The is operator in Python is used to check if two variables refer to the same object in memory. This is different from the == operator, which checks if two variables have the same value. In this lab, we will explore how the is operator works and how it can be useful in Python programming.This is an important concept to understand, especially when working with mutable objects like lists and dictionaries. In this lab, we will explore the usage of the is operator in Python with simple and complex examples, and see how it can be used to write efficient and reliable code.
Basic Usage
In this step, we will start with the most basic usage of the is operator.
- Open the Python Interpreter in the terminal.
python3
- Define two variables,
aandb, and assign the same value to both.
a = "Hello"
b = "Hello"
- Print out the memory address of
aandbusing theid()function.
print(id(a))
print(id(b))
- Use the
isoperator to check ifaandbrefer to the same object.
print(a is b)
The output of the above code should be True, since a and b refer to the same object in memory.
Immutable vs Mutable Objects
In this step, we will explore how the is operator behaves with immutable and mutable objects.
- Define two variables,
candd, and assign them both an empty list[].
c = []
d = []
- Print out the memory address of
canddusing theid()function.
print(id(c))
print(id(d))
- Use the
isoperator to check ifcanddrefer to the same object.
print(c is d)
The output of the above code should be False, since c and d are mutable objects and they are not the same object in memory.
- Now, define two variables,
eandf, and assign them both an integer value.
e = 10
f = 10
- Print out the memory address of
eandfusing theid()function.
print(id(e))
print(id(f))
- Use the
isoperator to check ifeandfrefer to the same object.
print(e is f)
The output of the above code should be True, since e and f are immutable objects and they share the same object in memory.
Function Parameters
In this step, we will explore how the is operator can be used with function parameters.
- Define a function
my_function()that takes two parameters,xandy.
def my_function(x, y):
print(x is y)
- Call the function
my_function()twice, once with immutable objects and once with mutable objects.
my_function(10, 10)
my_function([], [])
The output of the first call should be True and the output of the second call should be False.
Advanced Usage
In this step, we will explore some advanced usage of the is operator.
- Define a class
MyClasswith an attributevalue.
class MyClass:
def __init__(self, value):
self.value = value
- Define two instances of the
MyClassclass,obj1andobj2, with the samevalue.
obj1 = MyClass("Hello")
obj2 = MyClass("Hello")
- Print out the memory address of
obj1.valueandobj2.valueusing theid()function.
print(id(obj1.value))
print(id(obj2.value))
- Use the
isoperator to check ifobj1.valueandobj2.valuerefer to the same object.
print(obj1.value is obj2.value)
The output of the above code should be True, since the value attribute is an immutable object and both instances share the same object in memory.
- Now, define two instances of the
MyClassclass,obj3andobj4, with a mutable object as theirvalue.
obj3 = MyClass([])
obj4 = MyClass([])
- Print out the memory address of
obj3.valueandobj4.valueusing theid()function.
print(id(obj3.value))
print(id(obj4.value))
- Use the
isoperator to check ifobj3.valueandobj4.valuerefer to the same object.
print(obj3.value is obj4.value)
The output of the above code should be False, since the value attribute is a mutable object and both instances do not share the same object in memory.
Summary
In this lab, we learned about the is operator in Python and how it can be used to check if two variables refer to the same object in memory. We explored the behavior of the is operator with both immutable and mutable objects, function parameters, and class instances. By understanding the is operator, we can write more efficient and reliable Python code.



