- [Instructor] In this self check exercise,
we have a couple of exercises for you to perform.
In the first one, we would like you to use a
list comprehension, using the techniques that you just saw
in the preceding video, to create a list of tuples,
in which you'll have the numbers from one
through five and their corresponding cubes.
So your list is going to look something like
what we show here, so one cubed is one,
two cubed is eight, three cubed is 27.
Now, we haven't of course shown you creating tuples
with a list comprehension yet, so it is important
to point out that in order for you to create tuples,
the expression to the left of the for clause
has to have parentheses around it, and it would then
contain a comma separated list representing
the elements that would be placed into that tuple.
Now for the other exercise, we want you to use
a list comprehension and the range function,
with a step this time, to create a list of
the multiples of three that are less than 30.
So go ahead and pause the video and give those a shot,
and then come back to see the answers.
Okay, let's go ahead and reveal the
answer to this first exercise here.
Now again, we're trying to create tuples
based on the values in the range one through five.
So first, let's deal with the range part here.
For every item called X in the range one up to
but not including six, so one through five,
we're going to take that item, X,
and place it into a tuple of items, and the second element
in that tuple is going to be X's value cubed.
So we give X and X cubed, we wrap that in parentheses
because that is required when you're putting a tuple
to the left of the for keyword in a list comprehension,
and if we then go ahead and execute that and evaluate cubes,
we can see that indeed, we get the tuples
containing each value and its corresponding cube.
Now for the second exercise, again we wanted to create
a list of the multiples of three less than 30,
and we wanted to use a range with a step to do that.
So in this case, our for clause uses the range three
up to but not including 30, counting by three each time.
So for every value in that range, we're simply going to
include that value in our list, and we simply name the
target variable in order to include that value in the list.
So if I execute that cell and then evaluate multiples,
you can see we get all the multiples of three,
up to but not including 30.
So the last one is 27 in this case.