Changing a triple.
We have just learned that tuples are immutable but it can contain mutable objects.
Let us see an example.
So consider a tuple which has de following elements.
It also has a nested list which contains three elements.
As we know a tuple is immutable whereas a list is mutable so we can change the element in the list.
So how do we access the elements in the list.
We can do this by indexing so the nested list has an index for and for example I want to change the
element in the 0 8 position.
This is going to retrieve the string a.
Now I can go ahead and change this element to another string.
That's because lists are mutable.
So what is going to happen to the tuple.
Is it still going to point to the same memory location or since one of the elements in the nested list
has changed.
Valid point to another memory address so let's verify this using the idea function in Jupiter notebook.
Here we have a variable 2 3 which is assigned a tuple and this tuple Contains a nested list and before
making any changes to any of these elements in the tuple let's check the memory address to which this
variable is pointing to.
So this is the memory address and before making any changes to the nested list here.
I want to check the I.D. of this nested list so this would be 2 3 and the index is for so this nested
list is pointing to this memory location.
Now let us change one of the elements in the nested list.
So I'm going to give the index number 4 and yo I want to change the element in the 0 8 index.
To see another string.
If so let us see.
This assignment has changed the elements in the list so this assignment has changed.
The first element in the list as you can see here it is F B and C are the elements.
No now let us see if the list is still pointing to the all memory address.
So 2 3 of or so as you can see the list is still pointing to the all memory location.
That's because lists are changed in place.
Whenever we make a change to a list add an element or delete an element or or change an element.
A new list is not created.
Lists are changed in play.
So coming to the tuple.
So how does it took a look now after the nested list has been changed.
Here we have the tuple with the updated nested list.
So let us check the memory location to which the tuple is pointing to.
So this is the memory address to which it is pointing to after the changes have been made and this memory
address is same as the one it is pointing to before the changes have been made.
Now what would happen if we tried to change any of the other elements in the tuple Python has raised
an exception as you can see it has raised a type road and it says the tuple object does not support
item assignment meaning to build immutable objects we can use the plus operator to combine or concatenate
tuples.
And this is going to create a new tuple say for example we want to contact the native tuples to do an
2 Three
the new tuple that is created contains elements from both of these tuples the ones which are concatenated
and we can also repeat the elements in a tuple specified number of times using this operator.
And this is going to repeat these elements in the tuple two times as specified here.
The output generated in this case is a new triple the original tuple does not change as you can see
here.
It only contains only five elements and the one here after the petition contains a total of 10.