- [Instructor] In this Self Check exercise,
you'll use the techniques that you learned
in the preceding two videos to work with slices.
So, first we want you to start out by creating a list
called numbers with the values
from one through 15 in the list
then we want you to use slices
to perform the following four operations.
So first we want you to create a slice
that contains only the even integers.
Then we want you to replace the elements
at indices five through nine with zeros
and show the resulting list.
Then we want you to keep only the first five elements,
so delete the end of the list and show the result.
And finally we want you to delete all the remaining elements
by assigning to a slice and also display the result.
So go ahead and pause the video and give those a shot,
then come back to see the answers.
Okay, let's go ahead and start revealing the answers here.
We'll start out by creating the list called numbers
and you may recall that the list function is capable
of taking a sequence that you give it as an argument
and turning it into a list object.
So as a result of this operation, we create the list
starting from one up to, but not including, 16
and we can of course display
that to see the values from one through 15.
Now in part A here, we wanted you to select only
the even integers from the original list.
So if we look at that original list, we see that
the first even integer starts at index one,
which is why we have a one at the beginning of our slice.
We want to go all the way through the end of the list,
selecting those even elements.
So here we explicitly specify the length of the list numbers
but we could also just leave that empty
between the two colons and as you saw previously,
it will automatically figure out that we want
the entire list to be processed.
And then finally we have a step of two, saying we want
to go every other element throughout the list,
as we work our way along.
So, if we execute that, we can see that we get only
the even integers from that operation.
Next up we wanted in part B to replace the elements
at indices five through nine with zeros.
So here's the expression that will enable you to do that.
Remember that when you specify a subset slice,
you specify for the second index,
the one past the N position
so if you want to process five through nine,
you say 5:10, start from index five
and go up to, but not including, 10.
Now, you may recall,
that when you multiply a sequence by an integer,
you get that sequence repeated
the specified number of times.
I demonstrated that previously with a string
which gave us a new string containing the specified number
of iterations of whatever we were multiplying,
in our case it was an asterisk.
Here we're multiplying a list containing one zero element
by the length of this slice, numbers sub 5:10.
So number sub 5:10, gives me a sub list
for the index positions five, six, seven, eight and nine,
that's a five element sub list.
So what we're saying here is create a five element list
of zeros and assign it to those five elements
of the numbers list.
So if I go ahead and execute that,
then we can evaluate numbers
and see that those five elements are now all zeros.
Next up, we want in part C to keep only
the first five elements of the list.
So this is a way that you can express that,
so we're saying select the sub list
from index five through the end of the list
which is saying select everything
after the first five elements,
and replace all of those elements with nothing in this case.
So let's execute that
and then display the contents of numbers
and you can see we were able to keep
just the first five elements of that list.
And then finally, for part D we wanted
to delete all these remaining elements
by assigning to a slice
and in this case, whatever's left in the list,
this is going to select the entire copy of those elements
and replace those elements with nothing.
So let's execute that and finally if we evaluate numbers,
we can see that it is now empty.