1 00:00:00,090 --> 00:00:01,080 In the last lesson, 2 00:00:01,170 --> 00:00:05,010 we looked at how to provide default values for optional arguments. 3 00:00:05,340 --> 00:00:08,100 In this lesson, we'll discuss how to create functions 4 00:00:08,189 --> 00:00:10,290 that can take any number of arguments. 5 00:00:10,800 --> 00:00:13,680 Let's say that we had a function called add, 6 00:00:13,980 --> 00:00:18,660 which adds n1 and n2 and it just returns the sum of those numbers. 7 00:00:19,020 --> 00:00:23,850 That's very well and good, but what if I wanted to add more than two numbers? 8 00:00:23,940 --> 00:00:28,080 Well, I would have to add n1, n2, n3, n4, 9 00:00:28,320 --> 00:00:31,020 but then I would have to add four numbers every single time. 10 00:00:31,500 --> 00:00:36,500 So what if I wanted to make this function more flexible and to allow any number 11 00:00:37,080 --> 00:00:40,740 of arguments to be used as the input? Well, 12 00:00:40,830 --> 00:00:45,750 what I can do is to simply change the code to use this asterix 13 00:00:45,900 --> 00:00:48,060 and then the name of my parameter. 14 00:00:48,810 --> 00:00:52,110 That name args is by convention 15 00:00:52,140 --> 00:00:57,120 what most Python developers will use. And it simply stands for arguments. 16 00:00:57,420 --> 00:01:00,120 But you don't have to stick with that naming if you don't want to. 17 00:01:01,230 --> 00:01:05,430 But what you do need is that asterix. That's the really important part. 18 00:01:05,910 --> 00:01:10,910 What that tells Python is this function add can accept any number of arguments. 19 00:01:13,110 --> 00:01:15,210 And once inside the function, 20 00:01:15,240 --> 00:01:17,850 you can actually loop through all of the arguments 21 00:01:18,090 --> 00:01:22,770 which is going to be in the form of a tuple and you can do whatever it is you 22 00:01:22,770 --> 00:01:24,690 want with each of those arguments. 23 00:01:25,140 --> 00:01:29,520 Instead of now passing in only two arguments because that was fixed 24 00:01:29,520 --> 00:01:34,200 when we created the function, we can now pass in any number of values 25 00:01:34,290 --> 00:01:36,660 like four values or seven values, 26 00:01:36,960 --> 00:01:41,640 and we can do whatever it is we want with them. Let's try this code out. 27 00:01:41,670 --> 00:01:44,940 I'm going to delete the code related to Python turtle, 28 00:01:45,300 --> 00:01:49,980 and I'm going to create a new file called playground.py. 29 00:01:50,640 --> 00:01:54,720 And we're going to use this playground.py to test out what we've just learned. 30 00:01:55,560 --> 00:01:59,820 I want you to have a think about what you just saw in the slides and see if you 31 00:01:59,820 --> 00:02:04,820 can create a function called add where you can pass in as many numbers as you 32 00:02:05,610 --> 00:02:06,420 want 33 00:02:06,420 --> 00:02:11,420 and it will always add together all of the numbers that are being passed into 34 00:02:12,060 --> 00:02:16,080 the function as the input, and then return the total 35 00:02:16,080 --> 00:02:19,470 sum. Pause the video and see if you can give that a go. 36 00:02:21,210 --> 00:02:21,600 All right. 37 00:02:21,600 --> 00:02:26,600 So what we want to do is to be able to call this method add, for example 38 00:02:26,790 --> 00:02:30,270 down here and pass in any number of values, right? 39 00:02:30,300 --> 00:02:32,550 Let's just go with three to begin with. 40 00:02:33,090 --> 00:02:38,090 And then we want to receive all of those values inside this function and then 41 00:02:38,400 --> 00:02:42,300 add them all together. So we're going to use that asterix trick. 42 00:02:42,450 --> 00:02:47,130 So one * and then we can give a name to the arguments that we receive, 43 00:02:47,490 --> 00:02:49,860 which normally is just called args. 44 00:02:51,450 --> 00:02:53,280 Now that we've got these args, 45 00:02:53,490 --> 00:02:58,490 if I actually go ahead and print it out and we go to run and then run and change 46 00:03:01,750 --> 00:03:06,640 this to our playground, you can see it's basically just printing out a tuple. 47 00:03:07,090 --> 00:03:11,410 And that tuple represents all the inputs that were passed in when this function 48 00:03:11,410 --> 00:03:13,900 was called. And in fact, 49 00:03:13,900 --> 00:03:18,840 if I do a time check on this args, you can see it 50 00:03:18,840 --> 00:03:21,850 is indeed a tuple. Now, 51 00:03:21,850 --> 00:03:26,140 once we've got hold of this args tuple, we can actually loop through it. 52 00:03:26,320 --> 00:03:30,610 So we can say for n in args, for 53 00:03:30,640 --> 00:03:35,260 each of the numbers in that tuple, let's go ahead and print each of them. 54 00:03:36,280 --> 00:03:39,940 You can see it's now separated them all out; three, five, and six. 55 00:03:40,270 --> 00:03:42,910 So that makes it easy enough for us to create the sum. 56 00:03:43,240 --> 00:03:45,700 So let's create a sum which is equal to zero. 57 00:03:46,060 --> 00:03:48,550 And then instead of printing out each of the n, 58 00:03:48,580 --> 00:03:53,560 we can just add the n to sum, and finally outside the loop 59 00:03:53,590 --> 00:03:55,600 we'll return our sum. 60 00:03:56,350 --> 00:04:00,880 So now when we print this final results by calling add three, five, 61 00:04:00,880 --> 00:04:04,750 six, you can see it's now added three, five, and six, 62 00:04:05,020 --> 00:04:09,220 and we can call this method in different ways. We can add more values to it, 63 00:04:09,460 --> 00:04:13,630 two and one, and you can see it's now equal to 17. 64 00:04:13,990 --> 00:04:18,990 And you can basically keep going because it can take any number of values. 65 00:04:19,779 --> 00:04:23,710 This is all done by this asterix keyword. 66 00:04:24,280 --> 00:04:29,280 And it basically packs all of these numbers that you've put in as the input into 67 00:04:30,070 --> 00:04:33,790 a tuple called whatever it is you want it to be called. 68 00:04:34,120 --> 00:04:37,660 So you can call it numbers if you want, or you can stick to the default, 69 00:04:37,690 --> 00:04:42,400 which is just args. And then once we've got that args inside the function, 70 00:04:42,430 --> 00:04:47,080 we loop through it and we can do whatever we want to each of the values. Now, 71 00:04:47,110 --> 00:04:48,640 in addition to looping through it, 72 00:04:48,670 --> 00:04:53,200 you can also access them by index because it's a tuple after all. 73 00:04:53,200 --> 00:04:57,250 So we can say args at position zero like this. 74 00:04:57,280 --> 00:04:58,780 And if I print that out, 75 00:04:59,260 --> 00:05:03,550 you'll see that the value is three because the first one is three. 76 00:05:04,390 --> 00:05:08,260 So that's why these unlimited arguments using this * 77 00:05:08,290 --> 00:05:12,340 args is also known as unlimited positional arguments, 78 00:05:12,610 --> 00:05:17,320 because the position of your arguments which you pass into the function 79 00:05:17,350 --> 00:05:22,350 matters' a huge deal because if I fetched the one at position one, 80 00:05:22,630 --> 00:05:25,030 it will no longer be three, and it'll now be five. 81 00:05:26,140 --> 00:05:31,140 So this is a way for us to be able to define a function and specify a unlimited 82 00:05:34,300 --> 00:05:38,980 or unspecified number of inputs. In other words, 83 00:05:39,070 --> 00:05:43,690 the number of arguments for the function can vary because the asterix operator 84 00:05:43,930 --> 00:05:47,860 collects all of the arguments into a tuple. However, 85 00:05:47,890 --> 00:05:52,720 what if we wanted to refer to our arguments by name rather than by position? 86 00:05:53,230 --> 00:05:56,140 Well, that is exactly what we'll talk about in the next lesson. 87 00:05:56,460 --> 00:05:57,010 So I'll see you there.