- [Instructor] It's relatively common
in Python to have functions that can receive
an arbitrary number of arguments.
In fact, we've already used a couple
of those functions in Python.
For example, we used the min function
to be able to do things like this
where we could pass any number of arguments we want
and it will figure out the minimum of those arguments.
Now, the way that you define such a function in Python
is by using an asterisk on a parameter name.
So let's show you what we're talking about here.
So let's say we want to define an average function
that's capable of taking the average of any number
of numeric values that you pass into the function.
You can write a parameter of this form,
where the word args is just a convention, not a requirement,
and let me go ahead and write out the rest of the function
and then we'll talk about this.
So we want to return the sum of the arguments
and we want to divide that by the length of that parameter,
which is going to be the number of total arguments.
So what happens when you apply the asterisk
to a parameter name is this tells the Python interpreter
when somebody calls this function,
pack up all of the arguments into a tuple
and pass that tuple into the function definition.
So args is actually going to be a tuple
of whatever arguments are passed to this function.
Now, we've seen previously that when you pass a sequence
to the sum function, it can calculate the sum
of all the numbers in that sequence
and we've seen that when you pass a sequence
to the len function, it can tell you
how many elements are in that sequence.
So let's go ahead and define that function
and now we can go and calculate the average
of a couple of values.
So let's say we give it five and 10,
and it gives me back the average of 7.5.
Let's recall that and add in another value,
and of course the average of these three values
happens to be 10
and let's recall it and add in another value
just to show you that, once again,
we can calculate the average of those values.
Now, I'm not doing any error checking up here
so it is possible I can get an error with this as well.
For example, if I call average with no arguments,
then it's going to result in a zero division error
because of the fact that I didn't check
for divide by zero.
If the length of the tuple is zero,
there are no elements and therefore,
I wind up with the error that you see down below here.
Now, if you want to use this capability,
you can do that in any function,
including functions with arguments,
with required arguments or arguments
that have default values,
but no matter what, this particular argument
must be the last one in the parameter list.
Now, one interesting thing about Python
is an operator that enables you to take a sequence of values
and unpack that sequence so that the values
can be passed into a function.
So, for example, the average function
that we defined up above here
is capable of receiving a whole bunch of arguments
that perhaps are stored in a list already.
So, as we demonstrated in some earlier examples,
if you want to calculate a class average,
you are going to total up some grades
and then divide by the number of students.
Well, let's assume that we already have a list of grades.
Let's call it grades,
and let's go ahead and put some values in there,
so let's do 88 and 75
and 96 and 55 and 83.
So, of course, we've already seen
that we can do things like call the sum function
with grades as an argument and calculate
the sum of these values.
We've also seen that we can use the len function
to figure out the number of values in this list
and, of course, we can use that to calculate the average
but we already have that defined up here
in our average function.
So, what we can do instead
is call the average function and we can say,
"Unpack the elements of the list called grades
"and pass those into the average function."
So this is the equivalent of literally passing in
this highlighted, comma-separated list of values
as the set of arguments to the average function
and indeed, it gives me back the class average in that case.