# Mutable and Immutable in Python

# 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 **<mark>objects</mark>** <mark>in Python which can be changed.</mark>

## 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 **<mark>objects</mark>** <mark>in Python which cannot be changed</mark>.

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.

```python
username = "chai lover"
```

A variable **username** stores a string "chai lover". Lets try to change it.

```python
username = "chai aur code"
print(username)
```

Output:

```plaintext
'chai aur code'
```

Wait, we weren't supposed to change it. Strings are **<mark>Immutable Objects</mark>**. 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.

```python
x = 10
y = x    # y = 10
x = 15
print(y)
```

Output:

```plaintext
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:

```python
username = "chai lover"
```

In many other languages like C, C++, etc., variables are **containers**, but in Python, its a little bit different.

<mark>In Python, variables are</mark> **<mark>pointers</mark>** <mark>to a</mark> **<mark>memory address</mark>**<mark>.</mark>

The variable **username** <mark>points</mark> to the memory address '1000' which stores a value 'chai lover'.

![](https://cdn.hashnode.com/uploads/covers/6aecaa207543afaac85e3a40/7c91bc02-f487-4dd5-8ca5-94f25754dbbe.png align="center")

Now, what happens when we change the username?

```python
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.

**<mark>Note:</mark>** <mark>In Python, when there is nothing pointing to a memory location, the garbage collector cleans it automatically.</mark>

![](https://cdn.hashnode.com/uploads/covers/6aecaa207543afaac85e3a40/ed55a5fa-74a9-4b0f-a75c-88b5b6e56ab9.png align="center")

Well, now you know why 'chai aur code' was printed without any error.

<mark>The original value never changes, instead a whole new reference is made. This is what immutability in Python means.</mark>

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

```python
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.

![](https://cdn.hashnode.com/uploads/covers/6aecaa207543afaac85e3a40/a9d2e23c-d377-4f87-a397-3b781b19255c.png align="center")

```python
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.

**<mark>Note:</mark>** <mark>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.</mark>

![](https://cdn.hashnode.com/uploads/covers/6aecaa207543afaac85e3a40/f6db30c7-97dd-4b9c-850e-6c9c5e93821c.png align="center")

Now we know why 10 gets printed on the screen.

# Assignment: Proof of Immutability

Run this script yourself:

```python
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 **<mark>memory allocation</mark>** 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 ❤️
