[No audio]
Some functions in Python, such as the
len function, get only one argument.
If you try to pass more than one, it will
return an error, saying that it takes exactly one argument.
Some other functions, such as isinstance,
take exactly 2 arguments, 2.
If you try to pass 3 arguments, or
just 1, you will get an error.
Some other functions, such as print,
[No audio]
take an indefinite
number of arguments and you saw how we can
define such functions with only one or more argument.
But I haven't still explained how
to create these kinds of functions.
Let's create one define, let's call this mean.
So to calculate the mean of a series of numbers.
So imagine this was our function.
We want to pass as many items as we want.
We actually did create a mean function
earlier, which we could call like that,
but it still got only one argument.
So it was a list, like that.
It's still one object, one argument.
In this case, we don't want to pass a list there.
We want to pass as many items as we want.
So mean, and then what you do,
you use the asterisk and then args.
This can be anything actually, after the Asterisk, any
variable name would work.
But it's a good practice
because every programmer uses args.
So it's good that you use args
so others can read your code.
And you also get used to reading others
code by using args all the time.
You know what the others are talking about.
Now let me show you something before I
apply the algorithm that calculates the mean.
[Author typing]
return args, let's see what we get here.
Now, let me call the function.
[Author typing]
So 1, 3, you can also pass
strings to whatever you like, like that.
[Author typing]
And what you get is actually a tuple
containing all the items, all the objects that
you passed in the function call.
So that actually
[Author typing]
is a tuple.
[No audio]
So what can we do with that tuple?
Well we can calculate the sum of args.
So args is a variable, not asterisk args
divided by the length of the args.
In this case, we would get an
error because we have a there.
So remove a from there and try again.
a is a string, so it doesn't make
sense to have the average of a string.
So we just created a function with
an arbitrary number of known keyword arguments
only. I say only because you
cannot have keyword arguments here.
You see, x equal to 3 would not work here.
If you want keyword arguments, then see the next video.
[Outro sound]