
Introduction
Why do People Change?
-Divye Kapoor
People say people change, People change is why people change. Ah! I am still confused my Lord, Why do people change? And this loop continues, until its out of range. Ah! I am still confused my Lord. Why do people change?
We talked just yesterday, Promised for a heart-to-heart exchange, Now they see me as unknown, To them, I am someone strange.
We made the buildings of love, Now we own just a barren grange. Ah! I am still confused my Lord, Why do people change? And this loop continues, until its out of range. Ah! I am still confused my Lord. Why do people change?
Well, people change because they are Mutables.
Mutables
Mutable comes from the word Mutate. Pretty obvious isn't it? Now, what does Mutability mean? The ability to change. Yes, you guessed it right mutable objects are nothing but those objects in Python which can be changed.
Immutables
Why am I even telling it to you at this point, you have already skipped this part as you know what is in there. But, somehow if you haven't skipped:
Immutables are the objects in Python which cannot be changed.
Apart from all this, when did you last see someone starting off with a self written poetry? Yeah, that's me for you, a cup of tea would work for an appreciation 🙂.
Some Mind Boggling
Look at the list in the cover image, most of the datatypes in Python are Immutable. Lets work with Integers and Strings for a moment.
username = "chai lover"
A variable username stores a string "chai lover". Lets try to change it.
username = "chai aur code"
print(username)
Output:
'chai aur code'
Wait, we weren't supposed to change it. Strings are Immutable Objects. Is the list wrong by any chance?
No, the list is perfect. Your understanding of mutable and immutable in Python is incomplete. Lets fix that real quick.
x = 10
y = x # y = 10
x = 15
print(y)
Output:
10
How is this happening? y was supposed to be equal to x.
How Variables are stored in Python
This is where the ultimate clarity lies. Many beginners never get to learn such an internal depth and hence are never able to understand mutables and immutables in Python in depth. It is a very crucial topic and anyone learning Python must understand it. For this, you need to share this article so that I can be famous a lot of people learn the right way.
The String Problem
Referring to our previous code snippet:
username = "chai lover"
In many other languages like C, C++, etc., variables are containers, but in Python, its a little bit different.
In Python, variables are pointers to a memory address.
The variable username points to the memory address '1000' which stores a value 'chai lover'.
Now, what happens when we change the username?
username = "chai aur code"
A new reference to the memory, a new value 'chai aur code' gets stored at another random memory address (say 1009) and username starts pointing to it.
Note: In Python, when there is nothing pointing to a memory location, the garbage collector cleans it automatically.
Well, now you know why 'chai aur code' was printed without any error.
The original value never changes, instead a whole new reference is made. This is what immutability in Python means.
The Integer Problem
Now, when we understand how it works, we can decipher this section quite easily.
Let us quickly look at the code snippet we used:
x = 10
y = x # y = 10
Try to make the memory allocation diagram yourself for this piece of code first. Exactly, x points to some location in memory that stores 10 and y points at the same.
x = 10
y = x # y = 10
x = 15
print(y)
Make the diagram again. BINGO! x starts pointing to a new memory address which stores 15 while y remains at 10.
Note: Because y is still pointing to 10, so, it remains as it is in the memory. The garbage collector won't clean it until all the pointers are gone.
Now we know why 10 gets printed on the screen.
Assignment: Proof of Immutability
Run this script yourself:
x = 10
y = x
print("Memory Address of x: ", id(x)) # id() prints the memory address of a variable
print("Memory Address of y: ", id(y))
x = 15
print("Memory Address of x: ", id(x))
print("Memory Address of y: ", id(y))
Does x have 2 different memory addresses?
It proves that x left 10 and started pointing to some other memory location which stores 15 while y remained where it was.
Hence Proved!
Final Verdict
Some people relate mutable and immutable with constants and variables. This assumption is completely wrong (offer such people a cup of tea and this article to clarify their concepts). Instead, it refers to the memory allocation in Python.
Thanking You
I hope you enjoyed this article and are waiting for more. I will soon come up with another. All the credits go to Hitesh Sir for uploading this series on YouTube. A big thanks to him.
Go, drink some tea, SLURPPPPP!!!!
Written by Divye Kapoor with ❤️
Comments
Join the discussionNo comments yet. Be the first to comment.
Chai aur Python
This series contains everything I learnt while watching Chai aur Python by Hitesh Sir on YouTube

