Let us now discuss about the concept immutability in Python.
The ready builds.
Act like pointers.
No other than containers.
So what do we mean by this say for example in A C programming language when we assign a likelihood to
a variable the variable functions like a container that stores do well.
So this assignment actually creates a block of memory space so that it can hold the value for that relevant.
Now let's create another way to win B read B and assign oh where double E to variable b and this assignment
again create a block of memory space so that the variable B can hold the value in coming to the Python
language
here in Python we create available C by assigning a value twenty so we have just just cause that in
Python the variables act as point does to the memory locations the variable C will bind to a memory
location which contains the value twenty now let's define another variable D which is now assigned to
where the B C.
So what does this mean in python in python.
This assignment here means variable D point to the same value to which variable C is pointing to the
variable C and variable D are pointing to the same memory location now as part of the program say we
have assigned a new low to the variability C variable C now is equal to 40.
So what does Python do now.
Does it change the value in this particular memory location.
No
Python now makes the pointer variable C to point to the new value forty that is debatable D is still
pointing to the old value twenty so the content in the memory location are not changed.
The pointer is now pointing to a different memory location after the new assignment.
So the content of the objects of immutable data types can not be changed after the ad created so this
is about immutability of all these data types integer and flawed.
So here I am taking a variable C which is binding to devalue 10.
Now execute do code in this setting.
By hitting shift and into these next
I would like to check the memory location to which the pointer C is pointing to.
So let's use the built in function idea and enter the object.
See execute descent.
So this is the memory location it is pointing to.
Now let define another variable which has the same value as C..
That means the DNC should point to the same memory location containing value 10.
Now let's verify that again I D.
Or D So or C and D are pointing to the same memory location.
Now let's change the value or for the variability to 20 executed and then
check for the memory location.
So as you can see this memory location is different from what D pointing to so is D still pointing to
10 or is it pointing to the new value that is 20.
Let's check that
so D is still pointing to the value 10 as you can see here.
But C is now pointing to the value 20 which is in this particular memory location.
So something is mutable only when we are able to change these values held in the memory location without
changing the memory location itself.
But another example then we have assigned a different value to the variable C it points to a different
memory location containing the new value so this means building data types like integer flawed etc.
are immutable will see a few more examples of strings as well.
Shortly.