I have already mentioned earlier that everything in Python are objects. We have already discussed scalar and non-scalar objects in the previous on The Basic Elements of Python. Now, we will dig a little deeper and understand about their mutability and immutability in Python.

In the most basic sense, mutable means the one that can be changed/replaced/modified and immutable means the one that can never be changed or modified. Out of all the objects let us first classify the ones which are  mutable and the ones which are the opposite.

Mutable or Immutable? Object Type
Immutable int
Immutable float
Immutable bool
Mutable list
Immutable string
Immutable tuple
Mutable dictionary

From the above table, it is pretty much clear that the basic objects like int, float and bool are immutable. This is because they are non-divisible and atomic. Now, removing this three, we remain with sting and tuple as immutable object type. While, list and dictionaries are mutable object type. Thus, we have got two sequences which are mutable and two which are not. So, let us see how are these sequences mutable or immutable but in order to understand this, we will also require to learn about the working of id() and type() methods of Python.

id(), type()

Basically, id() returns with the identity of the object as an integer. This may resemble to reference to memory block ID on which that particular object is stored and type() simply returns with type of the object. Let us take an example to understand it with more clarity.

From the above example we can conclude one of the most important feature of Python, “PYTHON IS CONCERNED ONLY WITH THE VALUE ASSIGNED TO THE VARIABLE AND NOT WITH THE VARIABLE ITSELF.” In this example, we see that the id of a and c both is same as they both have same value but the id of a and b is different as they both have different values. If this same thing was implemented in some other programming language then remember that the id of a and c would be different as all the programming languages except Python allocates a new memory address to every new variable declared.

Let us take one more example to understand type().

type(), is very simple to understand from the above example. It is simply returning the type of the object. Now that id() and type() are clear let us go back to mutability.

Consider the following example where we take list L, string S and tuple T as given below and then we perform various operations in order to demonstrate that how are they mutable or immutable.

Now we do some operations.

Carefully see the above code and please keep a note of id of all the three sequences. We can see that we were easily able to update or replace the ‘K’ in L but not in S and T. When we tried to do so in S and T we were thrown with a TypeError Exception which informed us that item assignment is not allowed. This is because S and T are immutable. IMMUTABLE OBJECTS CANNOT BE MODIFIED OR UPDATED USING THEIR INDEX NEITHER THEY CAN BE UPDATED OR MODIFIED BY ANY OTHER WAY. But, what about the example below then ??

You can see that now we are able to modify as per requirement. I am now able to find a workaround now. What if Python doesn’t allow me to modify using index, I’ve got another way by which I can modify directly by changing the value in S and T by declaring them again. I guess now that as I did the task that I wanted to do by finding a workaround, I am now clever than Python Interpreter. Am I???

No, not at all. You can see the values of id for both S and T, now as well earlier, both are different. These means that, when I declared them again after changing the value they have a new id which means that this S and T are now the same as we had earlier. These are their new versions which are stored at different memory locations and also have different memory address. Thus, THEIR IMMUTABILITY PREVAILS as objects are called mutable only if you are able to modify them at their original memory location which is applicable to L as it’s id remains the same even after updating it. You can see it’s id below.

Hence, we can say that list is mutable whereas string and tuple are immutable. List provides mutability by the concept of aliasing. Mutability of list can be understood from the figure below.

Mutability of Lists

So guys, hope that mutability concept is clear now. In next post, I will write about functions as objects as well as some additional methods which are useful in Python.