Table of Contents
Toggle
Before knowing which types are immutable in Python, we have to know first of all, what is immutability? The basic meaning is that “cannot be changed or modified”. So in Python also we have that kind of data types in which elements cannot be changed once they are created. What are they? The answer is Tuples and Frozen sets. Become a Data Scientist with 360DigiTMG Data Science course in Hyderabad. Get trained by the alumni from IIT, IIM, and ISB.
To understand mutable and immutable data let’s have a simple example:
A=55;
B=id(A) #140725046277568
Here A stores a value of 55 and B is storing the memory location of A.
Now, we do this:
A=40
B=id(A)
What will be the values of A and B now?
A is 40 and B is the same old 140725046277568. So one can quickly notice that A is changed from
55 to 40 But B which is a memory location that is not changed. Here we can say A is mutable and B is immutable.
Well, now we discuss the immutable data types in Python. To have better
Understanding, We compare them with the data types which are mutable. Before all it begins, Are you looking to become a Data Scientist? Go through 360DigiTMG’s PG Diploma in Data Science and Artificial Intelligence!
Let’s have a look, what are the data types that are available in Python:
- Lists
- Tuples
- Sets
- Dictionaries
- Frozen sets
- Adding element to the existing data structure:
- Removing an element from the existing data structure:
- Modifying the existing elements: