Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Immutable Sequence Types in Python
Immutable Sequence Types
Mutable and immutable objects are treated differently in python. Immutable objects are faster to access and are costlier to change because it needs the creation of a copy.
Whereas mutable objects are easy to change.
In other words, Immutable objects cannot be changed after their creation.Example of immutable sequence in python is Tuple().
In Python the data structure or we can say sequences are defined into two categories –
- Mutable Sequences
- Immutable Sequences
Mutable sequences can be changed after their creation while immutable sequences can’t be updated however there are some tricks to update the immutable sequences. So, due to above characteristics it can be referred as immutable sequences are read-only and mutable sequences are both read and write compatible.
>>> companies = (['tata','wipro'],'accenture') >>> companies (['tata', 'wipro'], 'accenture') >>> type(companies) <class 'tuple'> >>> companies='tcs' >>> companies (['tcs', 'wipro'], 'accenture') >>> companies='cognizant' Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> companies='cognizant' TypeError: 'tuple' object does not support item assignment