Python Mutable vs
Python Mutable vs
Immutable
Data Types
Mutable and Immutable Data Types in
Python
Mutable or immutable is the fancy word for
explaining the property of data types of being able
to get updated after being initialized. The basic
explanation is thus: A mutable object is one whose
internal state is changeable. On the contrary, once
an immutable object in Python has been created, we
cannot change it in any way.
o Lists
o Dictionaries
o Sets
o User-Defined Classes (It depends on the user to
define the characteristics of the classes)
Python immutable data types:
Mutability in Python
As we have already told, an immutable Python
object cannot be altered.
In Python, the integer data type is a prime case of
an immutable object or data type. Let's conduct an
easy experiment to understand this.
Let's first make two integer variables, x and y. The
variable y is a reference to the variable x:
Now, x and y both point towards the same location
in memory. In other words, both integer variables
should have the same ID returned by the id()
function. Let's confirm that this is true:
Code
Dictionary
Due to the mutability of dictionaries, we can modify
them by implementing a built-in function update or
using keys as an index.
Let's look at an illustration of that.
Code
Set
Due to the mutability of sets, we can modify them
using a built-in function (update).
See an illustration of that:
Code
float
Since the float object in Python is an immutable data
type, we cannot alter or update it. As we previously
learned, immutable objects shift their memory
address whenever they are updated.
Here is an illustration of that:
Code
1.# Python program to show that float is an immutabl
e data type
2.
3.float_ = float(34.5)
4.print('The memory address of float before updating:
', id(float_))
5.
6.# Modifying the float object by giving a new value to
it
7.float_ = float(32.5)
8.print('The memory address of float after updating: ',
id(float_))
Output:
The memory address of float before updating: 139992739659504
The memory address of float after updating: 139992740128048
String
Since strings in Python are immutable data
structures, we cannot add or edit any data. We
encountered warnings stating that strings are not
changeable when modifying any section of the
string.
Here is an illustration of that:
Code
1.# Python program to show that a string is an immut
able data type
2.
3.# Creating a string object
4.string = 'hello peeps'
5.
6.# Trying to modify the string object
7.string[0] = 'X'
8.print(string)
Output:
TypeError Traceback (most recent call last)
<ipython-input-3-4e0fff91061f> in <module>
3 string = 'hello peeps'
4
----> 5 string[0] = 'X'
6
7 print(string)
Tuple
Because tuples in Python are immutable by nature,
we are unable to add or modify any of their
contents. Here is an illustration of that:
Code
FrozenSet
Like sets, frozensets are immutable data structures.
While the set's components can always be changed,
this cannot be done with a frozenset. Therefore,
nothing can be added to or updated in the frozenSet.
Here is an illustration of that:
Code