- [Instructor] In this Self Check exercise
we'd like you to use some of the techniques
that you learned in the preceding video.
First we'd like you create a function called cube_list,
which is going to walk through the list
that it receives as a parameter,
and for each item in the list
is going to raise that item to the third power
and store the result back into that element of the list.
And then we want you to show what the contents
of the numbers array that you pass
into the function is after the function call.
Separately we'd also like you to use an empty list,
we said to call it characters,
and the += operator to show
what gets placed into that list
when you append the string Birthday
to the end of the list characters.
So go ahead and pause the video
and give those two exercises a shot
and when you come back I'll show you the answers.
Okay, let's go and reveal the definition
of the cube_list function.
So here we have a cube_list function that receives
a parameter named values.
By the way, for those of you who are new
to the Python programming language,
coming from languages with required type information
like Java and C and C++ and C#, again
there's nothing here that actually says
this has to be a list.
Now if you don't give it a list or a sequence of values
that this for loop can iterate through,
that's going to result in error messages
at run time and similarly, if you don't give it
something that's compatible with the exponentiation operator
that too could result in problems at run time.
But these problems that are possible are also
as a result of the flexibility
of the Python language itself.
So I can pass anything I want into this function
and as long as it's something that's compatible
with what I'm doing in this for loop,
then that item will work with my cube_list function.
So let's go ahead and define the function.
We're simply going to iterate through
each of the elements in the list called values
that we receive, or the sequence called values,
and for every item in that list,
we are going to modify the item
by raising it to the third power.
So let's then create our numbers list
that contains the values from one through 10,
as specified in the problem statement,
and let's call cube_list with those values.
Now if this worked correctly, numbers should contain
the cubes of these values, so we'll have
one up to 1,000 in the results,
and indeed we now have the cubed values.
Let's take a look at the second exercise
where we simply wanted to take the string Birthday
and concatenate its elements to a list.
So we said to start by creating an empty list
called characters, then we want to do
the append operation using the +=,
and again if the left side is a list
the right side must be some sort
of iterable sequence of elements.
So far the types of iterable sequences
we've looked at are strings, tuples and lists,
and in this case we have string, so whoops!
So let's execute that, and finally
let's evaluate characters so that we can see
the individual characters were separated out
into separate strings, one at a time,
each of those was appended to the empty list.