Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
151 views

Mutable Vs Immutable Objects in Python

This document discusses the differences between mutable and immutable objects in Python. It provides examples of how integers and strings are immutable, while lists are mutable. The document demonstrates that mutable objects like lists are passed by reference, so changes made to them within a function will be reflected outside. However, immutable objects like integers are passed by value, so changes only affect the local variable.

Uploaded by

sandman21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Mutable Vs Immutable Objects in Python

This document discusses the differences between mutable and immutable objects in Python. It provides examples of how integers and strings are immutable, while lists are mutable. The document demonstrates that mutable objects like lists are passed by reference, so changes made to them within a function will be reflected outside. However, immutable objects like integers are passed by value, so changes only affect the local variable.

Uploaded by

sandman21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

1 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

''' Example 1 '''


>>> x = "Holberton"
>>> y = "Holberton"
>>> id(x)
140135852055856
>>> id(y)
140135852055856
>>> print(x is y) '''comparing the types'''
True

''' Example 2 '''


>>> a = 50
>>> type(a)

2 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

<class: ‘int’>
>>> b = "Holberton"
>>> type(b)
<class: 'string'>

x = 10

x = y

3 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

id(x) == id(y)

id(y) == id(10)

x = x + 1

id(x) != id(y)

id(x) != id(10)

m = list([1, 2, 3])

n = m

4 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

id(m) == id(n)

m.pop()

id(m) == id(n)

5 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

tuple

def updateList(list1):
list1 += [10]

n = [5, 6]
print(id(n)) # 140312184155336

updateList(n)
print(n) # [5, 6, 10]
print(id(n)) # 140312184155336

6 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

def updateNumber(n):
print(id(n))
n += 10

b = 5
print(id(b)) # 10055680
updateNumber(b) # 10055680
print(b) # 5

7 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

8 of 9 11-05-2018, 17:46
Mutable vs Immutable Objects in Python – megha mohan – Medium https://medium.com/@meghamohan/mutable-and-immutable-side-of-py...

9 of 9 11-05-2018, 17:46

You might also like