In the last lesson,
we looked at how to provide default values for optional arguments.
In this lesson, we'll discuss how to create functions
that can take any number of arguments.
Let's say that we had a function called add,
which adds n1 and n2 and it just returns the sum of those numbers.
That's very well and good, but what if I wanted to add more than two numbers?
Well, I would have to add n1, n2, n3, n4,
but then I would have to add four numbers every single time.
So what if I wanted to make this function more flexible and to allow any number
of arguments to be used as the input? Well,
what I can do is to simply change the code to use this asterix
and then the name of my parameter.
That name args is by convention
what most Python developers will use. And it simply stands for arguments.
But you don't have to stick with that naming if you don't want to.
But what you do need is that asterix. That's the really important part.
What that tells Python is this function add can accept any number of arguments.
And once inside the function,
you can actually loop through all of the arguments
which is going to be in the form of a tuple and you can do whatever it is you
want with each of those arguments.
Instead of now passing in only two arguments because that was fixed
when we created the function, we can now pass in any number of values
like four values or seven values,
and we can do whatever it is we want with them. Let's try this code out.
I'm going to delete the code related to Python turtle,
and I'm going to create a new file called playground.py.
And we're going to use this playground.py to test out what we've just learned.
I want you to have a think about what you just saw in the slides and see if you
can create a function called add where you can pass in as many numbers as you
want
and it will always add together all of the numbers that are being passed into
the function as the input, and then return the total
sum. Pause the video and see if you can give that a go.
All right.
So what we want to do is to be able to call this method add, for example
down here and pass in any number of values, right?
Let's just go with three to begin with.
And then we want to receive all of those values inside this function and then
add them all together. So we're going to use that asterix trick.
So one * and then we can give a name to the arguments that we receive,
which normally is just called args.
Now that we've got these args,
if I actually go ahead and print it out and we go to run and then run and change
this to our playground, you can see it's basically just printing out a tuple.
And that tuple represents all the inputs that were passed in when this function
was called. And in fact,
if I do a time check on this args, you can see it
is indeed a tuple. Now,
once we've got hold of this args tuple, we can actually loop through it.
So we can say for n in args, for
each of the numbers in that tuple, let's go ahead and print each of them.
You can see it's now separated them all out; three, five, and six.
So that makes it easy enough for us to create the sum.
So let's create a sum which is equal to zero.
And then instead of printing out each of the n,
we can just add the n to sum, and finally outside the loop
we'll return our sum.
So now when we print this final results by calling add three, five,
six, you can see it's now added three, five, and six,
and we can call this method in different ways. We can add more values to it,
two and one, and you can see it's now equal to 17.
And you can basically keep going because it can take any number of values.
This is all done by this asterix keyword.
And it basically packs all of these numbers that you've put in as the input into
a tuple called whatever it is you want it to be called.
So you can call it numbers if you want, or you can stick to the default,
which is just args. And then once we've got that args inside the function,
we loop through it and we can do whatever we want to each of the values. Now,
in addition to looping through it,
you can also access them by index because it's a tuple after all.
So we can say args at position zero like this.
And if I print that out,
you'll see that the value is three because the first one is three.
So that's why these unlimited arguments using this *
args is also known as unlimited positional arguments,
because the position of your arguments which you pass into the function
matters' a huge deal because if I fetched the one at position one,
it will no longer be three, and it'll now be five.
So this is a way for us to be able to define a function and specify a unlimited
or unspecified number of inputs. In other words,
the number of arguments for the function can vary because the asterix operator
collects all of the arguments into a tuple. However,
what if we wanted to refer to our arguments by name rather than by position?
Well, that is exactly what we'll talk about in the next lesson.
So I'll see you there.