Mutable & Immutable Data Types

                Mutable & Immutable Data Types 


Mutable Data Types : Data Types (Variables) whose values can be changed or updated after they are created and assigned are called mutable..

Immutable Data Types : Data Types (Variables) whose values can not be changed or updated after they are created and assigned are called mutable..



Lists [ ]


In above Example you can see x is  list data type which consists of values 1,2,3,4 .. In position 0 of x  (position 0 is index value),  1 was present but then we assigned 100 in position 0 of x .. Hence value of position 0 is updated from 1 to 100 ... Hence , List data type is mutable in nature.. I will cover more details on index position of list data type in further blog...

Dictionary {Key:Value} :



In above Example you can see ips is dictionary data type which consists of key :values pair .. Inside Dictionary vBond , vSmart & vManages are Keys... And 1.1.1.1 , 2.2.2.2 & 3.3.3.3 are its values respectively ... Then we replaced/updated  the value of key vBond ... Hence, Dictionary data type is mutable in nature.. I will cover more details on dictionary data type in further blog ... 

Sets { }:




In above Example you can see x is  set data type which consists of values 1,2,3,4 ..Then we used add syntax to update the set & string a is added at the end ... Hence , Set data type is mutable in nature..  I will cover more details on sets data type in further blog ... 

Tuples ( ):



In above Example you can see x is tuple data type which consists of values 1,2,3,4 .. In position 0 of x  (position 0 is index value),  1 was present,  then we assigned 100 in position 0 of x but it thrown error that tuple does not support assignment .. Hence value of position 0 is cannot be updated from 1 to 100 ... Hence , Tuple data type is immutable in nature.. I will cover more details on list data type in further blog..

Usages 

Lists : Simple iterable collection of data where data need to modify frequently ..
Tuple : When Data need not be change..
Set : When Duplicating is not required..
Dict : Based on key & value pair where modifications are required.. In Rest APIs when we use json data modeling ,we use dictionary data type..






Comments