1
761views
Differentiate Mutable Data types and Immutable Data types.
1 Answer
1
13views

Mutable and Immutable Types

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

1. Immutable types

The immutable types are those that can never change their value in place. In Python, the following types are immutable:integers, floating point numbers, Booleans, strings, tuples. Let us understand the concept of immutable types. In order to understand this, consider the code below:

p=5

q=P

r=5
     #will assign 5, 5,5to p, q,r

p=10

r=7

q=r

After reading the above code, you can say that values of integer variables p, q,rcould be changed effortlessly. Since p, q,rare integer types, you may think that integer types can change values.

Immutable Types

  • integers
  • floating
  • booleans
  • strings
  • tuples

But hold:It is not the case. Let's see how.

  • You already know that in Python, variable-names are just the references to value-objects i.e., data values. The variable-names do not store values themselves i.e., they are not storage containers.

  • Now consider the Sample code given above. Internally, how Python processes these assignments is explained in Fig.

  • So although it appears that the value of variablep/q/ris changing;values are not changing "in place" the fact is that the variable-names are instead made to refer to new immutable integer object. (Changing in place means modifying the same value in same memory location.)

1

  • You can check/confirm it yourself using id( ). The id()returns the memory address to which a variable is referencing.

    p=5

    q=p

    r=5

    id(5) 1457662208

    id(p) 1457662208

    id(q) 1457662208

    id(r) 1457662208

  • Please note, memory addresses depend on your operating system and will vary in different sessions.

  • When the next set of statements execute, i.e.,

    p=10

    r=7

    q=r

then these variable names are made to point to different integer objects. That is, now their memory addresses that they reference will change. The original memory address ofp that was having value 5 will be the same with the same value i.e.,5 but p will no longer reference it. Same is for other variables.

2

Let us check their ids

>>>p=10
>>>r=7
>>>q=r
>>> id(10)
1457662288
>>> id(p)
1457662288
>>> id(7)
1457662240
>>> id(q)
1457662240
>>> id(r)
1457662240
>>> id(5)
1457662208
  • Now if you assign 5 to any other variable. Let us see what happens.

    t=5

    id(t)

    1457662208

  • Thus, it is clear that variable names are stored as references toavalue-object. Each time you change the value, the variable's reference memory address changes.

  • Variables are NOT LIKE storage containers i.e., with fixed memory address where value changes every time. Hence they are IMMUTABLE.

2.Mutable types

  • The mutable types are those whose values can be changed in place. Only three types are mutable in Python. - These are: lists, dictionaries and sets.

  • To change a member of a list, you may write:

    Chk=[2, 4, 6] 
    Chk [1] =40
    

It will make the list namely Chk as [2, 40, 6].

>>> Chk=[2, 4, 6]
>>> id(Chk)
150195536
>>> Chk[1]=40 
>>> id(Chk)*
150195536

Mutable Types

  • Lists
  • Dictionaries
  • Sets
Please log in to add an answer.