1 00:00:00,800 --> 00:00:02,940 - [Instructor] Now that you've seen how to implement 2 00:00:02,940 --> 00:00:06,930 a function that can receive an arbitrary argument list, 3 00:00:06,930 --> 00:00:09,330 we'd like you to define your own function 4 00:00:09,330 --> 00:00:13,060 called Calculate Product that receives an arbitrary number 5 00:00:13,060 --> 00:00:16,650 of arguments and returns the result 6 00:00:16,650 --> 00:00:19,420 of multiplying all of those arguments together, 7 00:00:19,420 --> 00:00:21,780 the product of those arguments. 8 00:00:21,780 --> 00:00:24,480 So go ahead and implement that function, 9 00:00:24,480 --> 00:00:28,270 and try calling it with 10, 20 and 30. 10 00:00:28,270 --> 00:00:32,590 Then I want you to use this Range calculation, 11 00:00:32,590 --> 00:00:36,030 which remember is going to create a sequence of values, 12 00:00:36,030 --> 00:00:39,340 and I want you to unpack that range of values 13 00:00:39,340 --> 00:00:43,410 as the argument to the Calculate Product function, 14 00:00:43,410 --> 00:00:45,620 and call the function with that. 15 00:00:45,620 --> 00:00:47,630 So go ahead and pause the video to give 16 00:00:47,630 --> 00:00:52,010 those two exercises a shot, and when you come back, 17 00:00:52,010 --> 00:00:53,233 you can see the answer. 18 00:00:57,890 --> 00:01:01,400 Okay, so first let's go ahead and define 19 00:01:01,400 --> 00:01:03,140 the Calculate Product function. 20 00:01:03,140 --> 00:01:06,610 So just like our Average function, we are specifying 21 00:01:06,610 --> 00:01:10,180 just one parameter, in this case, which is 22 00:01:10,180 --> 00:01:14,240 the star args parameter, indicating an arbitrary 23 00:01:14,240 --> 00:01:17,510 number of arguments, and to calculate the product 24 00:01:17,510 --> 00:01:20,640 of all those values, what we did here is we defined 25 00:01:20,640 --> 00:01:24,060 a local variable called Product Initialized to One, 26 00:01:24,060 --> 00:01:27,840 then for every one of the values in the tuple Args, 27 00:01:27,840 --> 00:01:31,780 we're going to iterate through that and multiply product 28 00:01:31,780 --> 00:01:36,010 by the current value during each iteration of the loop. 29 00:01:36,010 --> 00:01:38,985 When we finish that loop, we'll have the total product 30 00:01:38,985 --> 00:01:41,240 in the variable called Product, 31 00:01:41,240 --> 00:01:43,460 and we can simply return that. 32 00:01:43,460 --> 00:01:46,309 Now there's also functional style programming capabilities 33 00:01:46,309 --> 00:01:49,890 in Python that would enable you to implement 34 00:01:49,890 --> 00:01:53,490 a reduction like this in a single line of code, 35 00:01:53,490 --> 00:01:57,210 and we'll discuss items like that in later lessons. 36 00:01:57,210 --> 00:02:00,560 So that's the definition of our Calculate Product function. 37 00:02:00,560 --> 00:02:03,840 Let's take a look at the first call to calculate product, 38 00:02:03,840 --> 00:02:07,600 so here we have the three individual arguments, 39 00:02:07,600 --> 00:02:10,860 10, 20 and 30, these will be packed into a tuple 40 00:02:10,860 --> 00:02:14,789 that will get assigned to the Args parameter. 41 00:02:14,789 --> 00:02:16,960 Automatically, the interpreter will handle that. 42 00:02:16,960 --> 00:02:20,100 And then of course, we are iterating through each 43 00:02:20,100 --> 00:02:23,830 of those elements to access and use their values. 44 00:02:23,830 --> 00:02:28,800 So 10 times 20 times 30 gives me the product 6,000. 45 00:02:28,800 --> 00:02:32,400 Now, you may be wondering how to use the Range function 46 00:02:32,400 --> 00:02:34,400 as described by this exercise. 47 00:02:34,400 --> 00:02:38,340 You literally put the star in front of the call to range. 48 00:02:38,340 --> 00:02:41,780 So this is going to produce a sequence of values 49 00:02:41,780 --> 00:02:45,480 from one to six, incrementing by two each time, 50 00:02:45,480 --> 00:02:47,380 so we'll have one, three and five. 51 00:02:47,380 --> 00:02:50,710 And we're going to unpack those values and calculate 52 00:02:50,710 --> 00:02:53,110 the product of those values, and you can see 53 00:02:53,110 --> 00:02:56,913 one times three times five gives me the product 15.