Mutable vs Immutable in Python
Mutable vs Immutable in Python
Immutable in
Python
In Python, the term "mutable" and "immutable" refer to the ability
of an object to be changed after it is created.
Understanding the Concept
1 Mutable Objects 2 Immutable Objects
These are objects that These are objects that
can be modified after cannot be changed once
they are created, such as they are created, such as
lists and dictionaries. integers, floats, and
Example:- strings.
Example:-
my_list=[1,2,3]
st="hello"
my_list[0]=10
st=st+"world"
print(my_list)
print(st)
o/p:-10,2,3
o/p:-hello world
Mutable Data Types
Lists Dictionaries Sets
Elements can be added, removed, Key-value pairs can be added, Elements can be added or removed
or modified after creation. removed, or modified after after creation.
creation.
Immutable Data Types
Integers Floats
Cannot be changed after creation. Cannot be changed after creation.
Strings Tuples
Cannot be changed after creation. Cannot be changed after creation.