- [Instructor] Now that you've seen how to implement
a function that can receive an arbitrary argument list,
we'd like you to define your own function
called Calculate Product that receives an arbitrary number
of arguments and returns the result
of multiplying all of those arguments together,
the product of those arguments.
So go ahead and implement that function,
and try calling it with 10, 20 and 30.
Then I want you to use this Range calculation,
which remember is going to create a sequence of values,
and I want you to unpack that range of values
as the argument to the Calculate Product function,
and call the function with that.
So go ahead and pause the video to give
those two exercises a shot, and when you come back,
you can see the answer.
Okay, so first let's go ahead and define
the Calculate Product function.
So just like our Average function, we are specifying
just one parameter, in this case, which is
the star args parameter, indicating an arbitrary
number of arguments, and to calculate the product
of all those values, what we did here is we defined
a local variable called Product Initialized to One,
then for every one of the values in the tuple Args,
we're going to iterate through that and multiply product
by the current value during each iteration of the loop.
When we finish that loop, we'll have the total product
in the variable called Product,
and we can simply return that.
Now there's also functional style programming capabilities
in Python that would enable you to implement
a reduction like this in a single line of code,
and we'll discuss items like that in later lessons.
So that's the definition of our Calculate Product function.
Let's take a look at the first call to calculate product,
so here we have the three individual arguments,
10, 20 and 30, these will be packed into a tuple
that will get assigned to the Args parameter.
Automatically, the interpreter will handle that.
And then of course, we are iterating through each
of those elements to access and use their values.
So 10 times 20 times 30 gives me the product 6,000.
Now, you may be wondering how to use the Range function
as described by this exercise.
You literally put the star in front of the call to range.
So this is going to produce a sequence of values
from one to six, incrementing by two each time,
so we'll have one, three and five.
And we're going to unpack those values and calculate
the product of those values, and you can see
one times three times five gives me the product 15.