1 00:00:00,570 --> 00:00:01,540 In this video, 2 00:00:01,540 --> 00:00:05,610 we're going to demonstrate the power of *args with a practical example. 3 00:00:05,610 --> 00:00:08,083 I've made a new file here in the workspace called cart.py. 4 00:00:08,083 --> 00:00:12,850 For this example, let's say we're building a simple shopping cart app. 5 00:00:12,850 --> 00:00:15,547 We'll write a function called calculate_total. 6 00:00:20,572 --> 00:00:23,744 The job of calculate_total is to add up the price of every item in 7 00:00:23,744 --> 00:00:24,760 the shopping cart. 8 00:00:25,760 --> 00:00:28,340 If I was coding this with positional arguments, 9 00:00:28,340 --> 00:00:33,900 I might add parameters like item1, item2, item three. 10 00:00:33,900 --> 00:00:35,000 But where do I stop? 11 00:00:35,000 --> 00:00:37,100 How do I know how many parameters to add? 12 00:00:37,100 --> 00:00:38,313 The short answer is I don't. 13 00:00:38,313 --> 00:00:40,630 And neither does our function. 14 00:00:40,630 --> 00:00:45,020 It can't know ahead of time how many items someone will add to their shopping cart. 15 00:00:45,020 --> 00:00:48,510 Using positional arguments for every item won't work. 16 00:00:48,510 --> 00:00:50,036 This is where *args comes in. 17 00:00:50,036 --> 00:00:53,600 By using *args as our parameter, we can pass as many 18 00:00:53,600 --> 00:00:57,500 items to the function as we want and the function will always work the same. 19 00:00:57,500 --> 00:00:58,372 So let's edit what I just did. 20 00:01:03,738 --> 00:01:05,717 Now that I've changed the parameter to *args, 21 00:01:05,717 --> 00:01:07,220 I can write the body of the function. 22 00:01:14,004 --> 00:01:18,594 On the right side of this assignment here, I'm passing args, in other words a tuple 23 00:01:18,594 --> 00:01:22,620 containing the price of every item in the shopping cart to the sum method. 24 00:01:23,890 --> 00:01:27,774 I have no idea how many items this tuple will contain when calculate_total function 25 00:01:27,774 --> 00:01:31,435 is called, but I don't need to because it works the same whether it's 1 item or 26 00:01:31,435 --> 00:01:32,103 20 or 50. 27 00:01:32,103 --> 00:01:38,100 The sum method by the way receives a sequence usually comprised of integers. 28 00:01:38,100 --> 00:01:42,700 It adds up the value of every item in that sequence and then returns the sum. 29 00:01:42,700 --> 00:01:45,750 In this example, we're assigning that sum to the total variable, 30 00:01:45,750 --> 00:01:50,140 then we can print it, return it or do something else entirely with that value. 31 00:01:50,140 --> 00:01:52,961 For now, I'll choose to print it so we can easily see what's going on. 32 00:01:54,059 --> 00:02:01,500 Calling the calculate_total function looks just like any other function calls. 33 00:02:01,500 --> 00:02:02,838 So let's see an example. 34 00:02:08,917 --> 00:02:13,824 I'll pass four values to this function representing the prices of four imaginary 35 00:02:13,824 --> 00:02:15,470 items in my shopping cart. 36 00:02:15,470 --> 00:02:18,886 Let's say it's 25, 25, 20, and 30. 37 00:02:18,886 --> 00:02:22,010 Now, I'll save and run this, and we'll take a look. 38 00:02:22,010 --> 00:02:25,887 I can expect that the sum of all of these prices is gonna be $100, let's see. 39 00:02:33,087 --> 00:02:35,271 Yep, all we see here is that it printed 100, 40 00:02:35,271 --> 00:02:38,300 the sum of the four prices I passed to the function. 41 00:02:38,300 --> 00:02:41,411 But look, if I call calculate_total again and 42 00:02:41,411 --> 00:02:46,094 send five prices instead of four, the function will work just as well. 43 00:02:49,057 --> 00:02:52,040 Now the printer value is 200, as expected. 44 00:02:52,040 --> 00:02:53,815 And if I only wanna send one price, 45 00:03:00,826 --> 00:03:02,600 We see 25 is printed. 46 00:03:02,600 --> 00:03:04,320 This is the beauty of *args. 47 00:03:04,320 --> 00:03:07,490 You can build a function that is ready to handle whatever data is thrown at it. 48 00:03:08,700 --> 00:03:09,460 Awesome. 49 00:03:09,460 --> 00:03:11,990 Now it is time for you to try this on your own. 50 00:03:11,990 --> 00:03:15,430 Before moving on to the next video, give this exercise a try. 51 00:03:15,430 --> 00:03:18,640 Your instructions are to write a short function called Packer. 52 00:03:18,640 --> 00:03:23,050 Any argument sent to Packer should be packed up into a *args parameter. 53 00:03:23,050 --> 00:03:26,530 In the body of the function, do something with args like print it out or 54 00:03:26,530 --> 00:03:27,790 loop through it. 55 00:03:27,790 --> 00:03:29,770 Play around with calling the function as well. 56 00:03:29,770 --> 00:03:33,830 Pass different numbers of arguments to Packer and see what the outcome is. 57 00:03:33,830 --> 00:03:37,070 This is an opportunity to try things and see cause and effect. 58 00:03:37,070 --> 00:03:37,570 Have fun.