Mutable vs immutable

Mutable and immutable are English words meaning “can change” and “cannot change” respectively. The meaning of the words is the same in the IT context; i.e.

  • a mutable string can be changed, and
  • an immutable string cannot be changed

Python code:

String are immutable, if we run following code it throw an error

varstr=’string is immuttable’

varstr[1]=’h’
print(varstr[1])

Output:

TypeError: ‘str’ object does not support item assignment

Tuple are also immutable:

vartup=1,2,3,4,5

vartup[1]=’this is immutable’

print(vartup)

List are mutable:

vardict={1:’India’,2:’US’,3:’UK’}

vardict[1]=’Canada’

print(vardict[1])

Dictionary are mutable:

vardict={1:’India’,2:’US’,3:’UK’}

vardict[1]=’Canada’

print(vardict[1])

Comments 2

  • Hello there! This is my 1st comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. Can you suggest any other blogs/websites/forums that cover the same subjects? Thanks!

  • Greetings from Ohio! I’m bored at work so I decided to
    check out your blog on my iphone during lunch break. I enjoy the knowledge
    you present here and can’t wait to take a look when I get
    home. I’m amazed at how quick your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, amazing site!

Leave a Reply