1 00:00:00,930 --> 00:00:02,720 - [Instructor] It's relatively common 2 00:00:02,720 --> 00:00:05,580 in Python to have functions that can receive 3 00:00:05,580 --> 00:00:08,730 an arbitrary number of arguments. 4 00:00:08,730 --> 00:00:11,170 In fact, we've already used a couple 5 00:00:11,170 --> 00:00:13,420 of those functions in Python. 6 00:00:13,420 --> 00:00:16,020 For example, we used the min function 7 00:00:16,020 --> 00:00:19,220 to be able to do things like this 8 00:00:19,220 --> 00:00:22,360 where we could pass any number of arguments we want 9 00:00:22,360 --> 00:00:25,930 and it will figure out the minimum of those arguments. 10 00:00:25,930 --> 00:00:30,930 Now, the way that you define such a function in Python 11 00:00:30,950 --> 00:00:35,500 is by using an asterisk on a parameter name. 12 00:00:35,500 --> 00:00:37,850 So let's show you what we're talking about here. 13 00:00:37,850 --> 00:00:41,120 So let's say we want to define an average function 14 00:00:41,120 --> 00:00:45,640 that's capable of taking the average of any number 15 00:00:45,640 --> 00:00:49,490 of numeric values that you pass into the function. 16 00:00:49,490 --> 00:00:52,210 You can write a parameter of this form, 17 00:00:52,210 --> 00:00:56,410 where the word args is just a convention, not a requirement, 18 00:00:56,410 --> 00:01:00,480 and let me go ahead and write out the rest of the function 19 00:01:00,480 --> 00:01:02,190 and then we'll talk about this. 20 00:01:02,190 --> 00:01:05,410 So we want to return the sum of the arguments 21 00:01:05,410 --> 00:01:10,410 and we want to divide that by the length of that parameter, 22 00:01:10,450 --> 00:01:13,500 which is going to be the number of total arguments. 23 00:01:13,500 --> 00:01:16,660 So what happens when you apply the asterisk 24 00:01:16,660 --> 00:01:21,370 to a parameter name is this tells the Python interpreter 25 00:01:21,370 --> 00:01:23,600 when somebody calls this function, 26 00:01:23,600 --> 00:01:28,290 pack up all of the arguments into a tuple 27 00:01:28,290 --> 00:01:32,640 and pass that tuple into the function definition. 28 00:01:32,640 --> 00:01:35,240 So args is actually going to be a tuple 29 00:01:35,240 --> 00:01:38,820 of whatever arguments are passed to this function. 30 00:01:38,820 --> 00:01:41,770 Now, we've seen previously that when you pass a sequence 31 00:01:41,770 --> 00:01:44,540 to the sum function, it can calculate the sum 32 00:01:44,540 --> 00:01:46,650 of all the numbers in that sequence 33 00:01:46,650 --> 00:01:48,710 and we've seen that when you pass a sequence 34 00:01:48,710 --> 00:01:50,640 to the len function, it can tell you 35 00:01:50,640 --> 00:01:53,540 how many elements are in that sequence. 36 00:01:53,540 --> 00:01:56,280 So let's go ahead and define that function 37 00:01:56,280 --> 00:01:59,540 and now we can go and calculate the average 38 00:01:59,540 --> 00:02:00,840 of a couple of values. 39 00:02:00,840 --> 00:02:03,180 So let's say we give it five and 10, 40 00:02:03,180 --> 00:02:07,370 and it gives me back the average of 7.5. 41 00:02:07,370 --> 00:02:09,980 Let's recall that and add in another value, 42 00:02:09,980 --> 00:02:12,410 and of course the average of these three values 43 00:02:12,410 --> 00:02:13,830 happens to be 10 44 00:02:13,830 --> 00:02:16,400 and let's recall it and add in another value 45 00:02:16,400 --> 00:02:18,640 just to show you that, once again, 46 00:02:18,640 --> 00:02:21,640 we can calculate the average of those values. 47 00:02:21,640 --> 00:02:24,850 Now, I'm not doing any error checking up here 48 00:02:24,850 --> 00:02:28,160 so it is possible I can get an error with this as well. 49 00:02:28,160 --> 00:02:31,210 For example, if I call average with no arguments, 50 00:02:31,210 --> 00:02:35,000 then it's going to result in a zero division error 51 00:02:35,000 --> 00:02:36,940 because of the fact that I didn't check 52 00:02:36,940 --> 00:02:38,370 for divide by zero. 53 00:02:38,370 --> 00:02:41,340 If the length of the tuple is zero, 54 00:02:41,340 --> 00:02:43,490 there are no elements and therefore, 55 00:02:43,490 --> 00:02:46,880 I wind up with the error that you see down below here. 56 00:02:46,880 --> 00:02:50,170 Now, if you want to use this capability, 57 00:02:50,170 --> 00:02:52,330 you can do that in any function, 58 00:02:52,330 --> 00:02:54,500 including functions with arguments, 59 00:02:54,500 --> 00:02:56,620 with required arguments or arguments 60 00:02:56,620 --> 00:02:58,040 that have default values, 61 00:02:58,040 --> 00:03:01,290 but no matter what, this particular argument 62 00:03:01,290 --> 00:03:04,963 must be the last one in the parameter list. 63 00:03:06,510 --> 00:03:09,590 Now, one interesting thing about Python 64 00:03:09,590 --> 00:03:14,590 is an operator that enables you to take a sequence of values 65 00:03:14,890 --> 00:03:19,090 and unpack that sequence so that the values 66 00:03:19,090 --> 00:03:21,580 can be passed into a function. 67 00:03:21,580 --> 00:03:23,960 So, for example, the average function 68 00:03:23,960 --> 00:03:26,030 that we defined up above here 69 00:03:26,030 --> 00:03:29,560 is capable of receiving a whole bunch of arguments 70 00:03:29,560 --> 00:03:32,610 that perhaps are stored in a list already. 71 00:03:32,610 --> 00:03:36,247 So, as we demonstrated in some earlier examples, 72 00:03:36,247 --> 00:03:39,330 if you want to calculate a class average, 73 00:03:39,330 --> 00:03:42,650 you are going to total up some grades 74 00:03:42,650 --> 00:03:45,590 and then divide by the number of students. 75 00:03:45,590 --> 00:03:49,510 Well, let's assume that we already have a list of grades. 76 00:03:49,510 --> 00:03:51,200 Let's call it grades, 77 00:03:51,200 --> 00:03:53,500 and let's go ahead and put some values in there, 78 00:03:53,500 --> 00:03:56,750 so let's do 88 and 75 79 00:03:56,750 --> 00:04:00,730 and 96 and 55 and 83. 80 00:04:00,730 --> 00:04:03,100 So, of course, we've already seen 81 00:04:03,100 --> 00:04:05,930 that we can do things like call the sum function 82 00:04:05,930 --> 00:04:08,560 with grades as an argument and calculate 83 00:04:08,560 --> 00:04:10,450 the sum of these values. 84 00:04:10,450 --> 00:04:13,330 We've also seen that we can use the len function 85 00:04:13,330 --> 00:04:15,980 to figure out the number of values in this list 86 00:04:15,980 --> 00:04:19,310 and, of course, we can use that to calculate the average 87 00:04:19,310 --> 00:04:22,170 but we already have that defined up here 88 00:04:22,170 --> 00:04:24,230 in our average function. 89 00:04:24,230 --> 00:04:26,400 So, what we can do instead 90 00:04:26,400 --> 00:04:30,287 is call the average function and we can say, 91 00:04:30,287 --> 00:04:34,367 "Unpack the elements of the list called grades 92 00:04:34,367 --> 00:04:37,710 "and pass those into the average function." 93 00:04:37,710 --> 00:04:41,960 So this is the equivalent of literally passing in 94 00:04:41,960 --> 00:04:45,180 this highlighted, comma-separated list of values 95 00:04:45,180 --> 00:04:48,500 as the set of arguments to the average function 96 00:04:48,500 --> 00:04:52,773 and indeed, it gives me back the class average in that case.