WEBVTT

0
00:00.090 --> 00:01.080
In the last lesson,

1
00:01.170 --> 00:05.010
we looked at how to provide default values for optional arguments.

2
00:05.340 --> 00:08.100
In this lesson, we'll discuss how to create functions

3
00:08.190 --> 00:10.290
that can take any number of arguments.

4
00:10.800 --> 00:13.680
Let's say that we had a function called add,

5
00:13.980 --> 00:18.660
which adds n1 and n2 and it just returns the sum of those numbers.

6
00:19.020 --> 00:23.850
That's very well and good, but what if I wanted to add more than two numbers?

7
00:23.940 --> 00:28.080
Well, I would have to add n1, n2, n3, n4,

8
00:28.320 --> 00:31.020
but then I would have to add four numbers every single time.

9
00:31.500 --> 00:36.500
So what if I wanted to make this function more flexible and to allow any number

10
00:37.080 --> 00:40.740
of arguments to be used as the input? Well,

11
00:40.830 --> 00:45.750
what I can do is to simply change the code to use this asterix

12
00:45.900 --> 00:48.060
and then the name of my parameter.

13
00:48.810 --> 00:52.110
That name args is by convention

14
00:52.140 --> 00:57.120
what most Python developers will use. And it simply stands for arguments.

15
00:57.420 --> 01:00.120
But you don't have to stick with that naming if you don't want to.

16
01:01.230 --> 01:05.430
But what you do need is that asterix. That's the really important part.

17
01:05.910 --> 01:10.910
What that tells Python is this function add can accept any number of arguments.

18
01:13.110 --> 01:15.210
And once inside the function,

19
01:15.240 --> 01:17.850
you can actually loop through all of the arguments

20
01:18.090 --> 01:22.770
which is going to be in the form of a tuple and you can do whatever it is you

21
01:22.770 --> 01:24.690
want with each of those arguments.

22
01:25.140 --> 01:29.520
Instead of now passing in only two arguments because that was fixed

23
01:29.520 --> 01:34.200
when we created the function, we can now pass in any number of values

24
01:34.290 --> 01:36.660
like four values or seven values,

25
01:36.960 --> 01:41.640
and we can do whatever it is we want with them. Let's try this code out.

26
01:41.670 --> 01:44.940
I'm going to delete the code related to Python turtle,

27
01:45.300 --> 01:49.980
and I'm going to create a new file called playground.py.

28
01:50.640 --> 01:54.720
And we're going to use this playground.py to test out what we've just learned.

29
01:55.560 --> 01:59.820
I want you to have a think about what you just saw in the slides and see if you

30
01:59.820 --> 02:04.820
can create a function called add where you can pass in as many numbers as you

31
02:05.610 --> 02:06.420
want

32
02:06.420 --> 02:11.420
and it will always add together all of the numbers that are being passed into

33
02:12.060 --> 02:16.080
the function as the input, and then return the total

34
02:16.080 --> 02:19.470
sum. Pause the video and see if you can give that a go.

35
02:21.210 --> 02:21.600
All right.

36
02:21.600 --> 02:26.600
So what we want to do is to be able to call this method add, for example

37
02:26.790 --> 02:30.270
down here and pass in any number of values, right?

38
02:30.300 --> 02:32.550
Let's just go with three to begin with.

39
02:33.090 --> 02:38.090
And then we want to receive all of those values inside this function and then

40
02:38.400 --> 02:42.300
add them all together. So we're going to use that asterix trick.

41
02:42.450 --> 02:47.130
So one * and then we can give a name to the arguments that we receive,

42
02:47.490 --> 02:49.860
which normally is just called args.

43
02:51.450 --> 02:53.280
Now that we've got these args,

44
02:53.490 --> 02:58.490
if I actually go ahead and print it out and we go to run and then run and change

45
03:01.750 --> 03:06.640
this to our playground, you can see it's basically just printing out a tuple.

46
03:07.090 --> 03:11.410
And that tuple represents all the inputs that were passed in when this function

47
03:11.410 --> 03:13.900
was called. And in fact,

48
03:13.900 --> 03:18.840
if I do a time check on this args, you can see it

49
03:18.840 --> 03:21.850
is indeed a tuple. Now,

50
03:21.850 --> 03:26.140
once we've got hold of this args tuple, we can actually loop through it.

51
03:26.320 --> 03:30.610
So we can say for n in args, for

52
03:30.640 --> 03:35.260
each of the numbers in that tuple, let's go ahead and print each of them.

53
03:36.280 --> 03:39.940
You can see it's now separated them all out; three, five, and six.

54
03:40.270 --> 03:42.910
So that makes it easy enough for us to create the sum.

55
03:43.240 --> 03:45.700
So let's create a sum which is equal to zero.

56
03:46.060 --> 03:48.550
And then instead of printing out each of the n,

57
03:48.580 --> 03:53.560
we can just add the n to sum, and finally outside the loop

58
03:53.590 --> 03:55.600
we'll return our sum.

59
03:56.350 --> 04:00.880
So now when we print this final results by calling add three, five,

60
04:00.880 --> 04:04.750
six, you can see it's now added three, five, and six,

61
04:05.020 --> 04:09.220
and we can call this method in different ways. We can add more values to it,

62
04:09.460 --> 04:13.630
two and one, and you can see it's now equal to 17.

63
04:13.990 --> 04:18.990
And you can basically keep going because it can take any number of values.

64
04:19.780 --> 04:23.710
This is all done by this asterix keyword.

65
04:24.280 --> 04:29.280
And it basically packs all of these numbers that you've put in as the input into

66
04:30.070 --> 04:33.790
a tuple called whatever it is you want it to be called.

67
04:34.120 --> 04:37.660
So you can call it numbers if you want, or you can stick to the default,

68
04:37.690 --> 04:42.400
which is just args. And then once we've got that args inside the function,

69
04:42.430 --> 04:47.080
we loop through it and we can do whatever we want to each of the values. Now,

70
04:47.110 --> 04:48.640
in addition to looping through it,

71
04:48.670 --> 04:53.200
you can also access them by index because it's a tuple after all.

72
04:53.200 --> 04:57.250
So we can say args at position zero like this.

73
04:57.280 --> 04:58.780
And if I print that out,

74
04:59.260 --> 05:03.550
you'll see that the value is three because the first one is three.

75
05:04.390 --> 05:08.260
So that's why these unlimited arguments using this *

76
05:08.290 --> 05:12.340
args is also known as unlimited positional arguments,

77
05:12.610 --> 05:17.320
because the position of your arguments which you pass into the function

78
05:17.350 --> 05:22.350
matters' a huge deal because if I fetched the one at position one,

79
05:22.630 --> 05:25.030
it will no longer be three, and it'll now be five.

80
05:26.140 --> 05:31.140
So this is a way for us to be able to define a function and specify a unlimited

81
05:34.300 --> 05:38.980
or unspecified number of inputs. In other words,

82
05:39.070 --> 05:43.690
the number of arguments for the function can vary because the asterix operator

83
05:43.930 --> 05:47.860
collects all of the arguments into a tuple. However,

84
05:47.890 --> 05:52.720
what if we wanted to refer to our arguments by name rather than by position?

85
05:53.230 --> 05:56.140
Well, that is exactly what we'll talk about in the next lesson.

86
05:56.460 --> 05:57.010
So I'll see you there.