Python Mutable and Immutable

Nikhil Pandey
1 min readMay 13, 2023

--

⏮️previous page

The python data objects can be broadly categorized into two- mutable and immutable types, in simple words changeable or modifiable and non-modifiable types.

mutable objects are : list, set, dictionary

Immutable Types: The immutable types are those that can never change their value in place. In python, the following types are immutable: integer, floating point numbers, Booleans, strings, tuples.

immutable objects : int, float, complex, string, tuples

Mutable Types: Mutability means that in the same memory address, new value can be stored as and when you want ,the type that do not support this property are immutable type.

Now consider some examples..

#illustrating list
>>>a=[1,2,3,4]
>>>a[2]=4
>>>a
[1, 2, 4, 4]

#illustrating tuple

>>>b=1,2,3,4
>>>b
(1, 2, 3, 4)
>>>b[2]=2
#error will be declared!

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
b[2]=2
TypeError: 'tuple' object does not support item assignment

>>> d={'fruit':'mango','colour':'blue'}
>>> d['colour']='yellow'
>>> d
{'fruit': 'mango', 'colour': 'yellow'}

That’ll for today! Bye (please mail me if your find anything to be improved. pandeynikhilone@gmail.com) or Have any suggestion! fill form

⏮️previous page

--

--

Nikhil Pandey

I am a student and a programming nerd. I learn and teach topics to help my fellow readers understand and apply their programming knowledge fluently.