Skip to main content

Mutable and Immutable Data T

Updated 1 min readMarkdown

Mutable

Objects whose data can be changed after creation are called Mutable objects.

Examples: List, Set, Dictionary, Byte array

a = [10, 20, 30]

a[0] = 100

print[a]

Output:

[100, 20, 30]

The same object is modified.

Immutable

Objects whose data cannot be changed after creation are called Immutable objects.

Examples: Integer, Float, Boolean, String, Tuple, Frozen set, Bytes

a = 10

b = 20

The old 10 object is not changed. a now refers to a new 20 object.

Internal Working

Mutable:

a--[10, 20]

[100, 20]

The same object is modified.

Immutable:

a--10

b--20

A new object/reference is used.

Key Point

Mutable = Can be changed

Immutable = Cannot be changed after creation

No comments yet. Be the first to comment.