Mutable vs Inmutable in Python

Valentina Jaramillo Ramirez
5 min readSep 30, 2020

First, in the Python language, the data takes the form of objects, these objects are a block of memory with values, and a variable is something that references to that piece of memory. The variable stores the address to that object, that is why you can reassign a variable pointing to different types of data.

As we see, everything is an object in Python. Even simple numbers, with values, and supported operations. These objects are classified as immutable or mutable.

id and type

Every object has its own identity that stores its address in memory and has his own type. In order to be able to see these characteristics within the object or instance, we use the built-in function id(), that returns the instance’s memory address of any object. Example:

On the other hand, the type object can be given by the built-in function type(), which returns the type of an object. Let’s look at an example:

Mutable and Immutable objects

At the start of this article, we already said that the Python objects can be classified as mutable (changeable) or immutable (unchangeable). In simple terms, the mutable objects can change their values at any time and place, and the immutable objects can guarantee that the object remains the same constant throughout a program. This distinction is crucial in Python work

Mutable objects:

lists, dictionaries, and sets, bytearray.

Immutable objects:

Numbers, complex, bool, strings, tuples, bytes, and frozen sets

Mutable and Immutable objects:

user-defined classes (Can be mutable or be specified as immutable)

Let’s do an example of Mutable objects with a list type object take a look into the address of the list and every object within it:

Now let's change an element of the list for another integer like 5 into the position 2 in the list:

As we could see the address of the list and every object in it, except for position 2, remains the same. However, position 2 now stores a different memory address that holds the value of 5

Valentina Jaramillo Ramirez

EN | SP Software Developer Student at Holberton School in Medellin, Colombia.