In python the concept of mutability and immutability works in a different way.
First of all what we understand when we read or listen about mutability and immutability , let me tell you about mine when I first read it i thought that mutable (things that can alter) and immutable (things that we can’t alter) but that what happen behind the scenes of python , Should we just move forward by understanding that much?
I think No , Python is beyond that simple grammatical meaning of two words. So what exactly happen is the concept we will explore in this blog , and believe it pretty easy to understand then it seems.
First look at this image (picked from the google).

This image is contains the list of data types in python and divided them on the basis of Mutable and Immutable datatypes.
Let pick Immutable to understand the concept because deep down we all know what happen with the mutable ones.
1.Strings :- Strings are considered as Immutable datatype so what happen when we declare a string in python. for example,
first_name = "Name1"
last_name = first_name
print(first_name)
print(last_name)
here we declared a variable first_name with the value “Name1”, then one more variable last_name with the value as first_name , which means the last_name variable also point to the same object that our first_name is pointing.
Now think it as in memory there is an object created of string type with the value “Name1” , now there is two variable that pointing it first_name and last_name , and our print statement will print the same value for the variable because we know both of them are pointing to same object.
first_name = "Name2"
print(first_name)
print(last_name)
Now add this code in your python file (if you are following with me) below to the previous code and execute the file.
Now you will see a change in the output , for the first print statement which showing value of first_name you will get output as “Name2” but for the last_name you are still getting the “Name1”. why or how?
This gave birth to the whole logic behind it that this example proves that the old value or object didn’t got mutated … I mean to say that the “Name1” is still in memory but you already override it by “Name2” then how? yes because Strings are immutable which means that if once you instantiate a string object with some value and assign it to a variable and then you reassign the same variable with some different value then the previous object didn’t get altered but in place of it a new object got created with the new value and got assigned to the variable.
This is the reason that our last_name still have the old value this proves that the old object is still in memory and have the same value .
Now this same fundamental approach work for every single Immutable datatype of python internally.
Now I hope it make more sense to you that why this string , integer etc are considered as Immutable.
In General , Beginner makes this mistake of understanding also Programmers from different programming language like if you know JavaScript then many programmers confuse the concept with ‘var’ and ‘const’ keyword of JavaScript which is not true.
So my Purpose of writing this is to help most of beginners who don’t know or misunderstood the concept at some point of life .
Again the same line I always follow “Engineering is to know about Behind the Scenes of things not just to know the things”.