Lists are mutable, you can change them,
you can reassign the contents at any position to be a different value.
So, in this example,
we're setting fruit as a variable to have the value banana, apple,
cherry and then we're going to change it so that
the first item isn't banana, it's pear instead.
Let's go through this, a line at a time and draw out the reference diagram.
On line one, we
create a variable fruit and assign it to a list with three items.
We have banana, we
have apple and we've got cherry.
If we print it out, we'll we get banana, apple and cherry.
Then we'll get to line four where we're going to
change the value in one of these positions.
If previously seen indexing into lists,
lists have positions like zero, one and two.
So, fruit square bracket zero is referring to this position that currently has banana.
On line four, we're not just referring to fruit square bracket zero,
we are changing the value of fruit square bracket zero.
We're making fruit square bracket zero equal to a new value.
So, that has the effect of replacing the old value with a new one,
we cross out banana and we add a new value pear.
Then we get to line five where we're going to make another replacement.
In this case, we're replacing at position minus one,
just as we did for looking up values in
a list by position zero one two or from the end position minus one,
position minus, two position minus three,
we can assign to those positions.
So, fruit square bracket minus one equals
something that's going to cause us to cross out,
replace the current value that's there with a new value,
in this case, orange.
So, let's try running this,
the first time something is printed,
we have the original values of banana, apple and cherry.
Then we made our two substitutions and then we printed something else.
We printed the new value of fruit which had pear, apple and orange.
Instead of just replacing a single item,
we can replace the items at
a particular slice using the same slice notation we used for looking up items,
we can use that to find a place where we want to do a replacement.
So, let's look at this one in codelens,
the first line when we execute it creates a variable called alist,
alist has, as its value,
this list of five or six items a, b, c, d,
e and f. Line two is going to mutate to destructively modify this list.
Now, remember that when we use the slice notation,
we're referring to a subsequence of the list
and we begin at position one and we go up to,
but not including position three.
So, this is a slice that's two items long and we're going to
replace those and get rid of that whole slice.
We're going to add in two new items there, x and y.
So, now when we print it out,
let's just see the net effect of that,
pure notations, we've now replaced the items at positions one and two with x and y.
If I print it, I now get a, x, y, d, e,
f instead of a, b, c,
d, e, f. Now,
we're not required to have the things that
we add in be of the same length as the things that are being replaced.
Here's an example showing how we can actually use this reassignment to delete items.
So, in this case, we first assign alist to have the values a,
b, c, d, e and f,
we're going to start at position one,
go up to position three,
but not including position three and we're going to delete
those items and replace them with the empty list.
We're going to replace them with all of the items that are
in this empty list which is nothing.
So, we're really going to just get a, d,
e and f. Sure enough when I go forward,
I get a, d,
e and f. Lists are mutable, strings aren't.
We could reassign to position zero in a list,
we cannot reassign to position zero in a string.
So, for example, if I run this code,
I set the variable greeting to be a string and then
I try to change the thing at position zero just analogous to what we were doing before,
we had reading,
bound to this string.
So, we have greeting bound to that string.
In position zero, we've got
that h and you might think that we could just replace the h with a j,
but we can't do that,
that is not permitted and we get this error message.
You get a message saying,
"Type string does not support item assignment,
we can't reassign a value within a string," Now,
suppose we really did want to take,
our Hello world string and turn it into Jello world.
How could we do that? We can't reassign at position zero,
that's what we found out just a moment ago. So, how do we do it?
Well, we can take the string Hello world and we can
make a new string that has some of Hello world,
but a little bit different.
In particular, we're going to take that
Hello world and we're
going to start at position,
so this position zero,
position one, two, three and so on.
We're going to start at one and go all the way to the end.
That's what it tells us when we say greeting,
square bracket one colon.
Greeting is the name of our variable
and we're going to start at position one that's with the letter
e and go all the way to the end because we don't have anything after the colon,
that means, keep going all the way to the end of the sequence.
So, that gives us Hello world and if we take
Hello world concatenate that
onto J we get Jello world.
Then we have Jello world.
Now notice, on line four,
it says print greeting.
Greeting is still Hello world.
So, that's why we get this is our second line.
New greeting was bound
to our new string Jello world and that's why we get this printed out.
So, you can't change the original string Hello world,
but you can make a new string using a lot of the letters from
Hello world and just concatenating them onto a new first letter.
Here's an example of concatenating together a bigger string from parts.
Let's show this one in codelens.
We first start with a small phrase,
many moons, we want to expand that.
So, we define a new string that has many moons plus some more letters.
So, we still have the old variable phrase,
but we have an additional variable now called phrase expanded.
We can keep doing that we can make it even longer.
That's what's going to happen in step three.
So, phrase larger is yet a longer string.
Notice that the previous variables are still bound to their original values.
Then finally, we're going to replace that small m with
a capital M. So,
you'll be assembling longer strings from shorter strings in this way and it
will let you put variables in the middle of
your strings to generate slightly different strings each time.