1 00:00:00,000 --> 00:00:04,619 [MUSIC] 2 00:00:04,619 --> 00:00:09,714 Hi again, welcome to the final stage of functions, packing and unpacking. 3 00:00:09,714 --> 00:00:12,876 We've talked a lot about the functions part of this course so far, 4 00:00:12,876 --> 00:00:14,360 how are you feeling? 5 00:00:14,360 --> 00:00:17,520 Do you feel confident in writing basic functions, passing arguments and 6 00:00:17,520 --> 00:00:19,100 receiving returns? 7 00:00:19,100 --> 00:00:22,270 If so, then keep moving forward to learn about packing and unpacking. 8 00:00:22,270 --> 00:00:27,220 If not, then before moving on, try to review the content in the course so far, 9 00:00:27,220 --> 00:00:31,115 check out the Treehouse community, and practice by writing your own functions and 10 00:00:31,115 --> 00:00:32,850 calling them to see how things work. 11 00:00:32,850 --> 00:00:34,480 So what's packing? 12 00:00:34,480 --> 00:00:39,216 Well, in the last section, we talked about passing multiple arguments to a function, 13 00:00:39,216 --> 00:00:43,710 and how we receive each of those arguments into its own parameter, or variable. 14 00:00:43,710 --> 00:00:48,652 Packing expands on this concept and let's us do something potentially more useful, 15 00:00:48,652 --> 00:00:51,132 and certainly more cool, let's dive in. 16 00:00:51,132 --> 00:00:54,862 The basic concept of packing is compressing multiple values into 17 00:00:54,862 --> 00:00:56,020 one tuple. 18 00:00:56,020 --> 00:00:58,136 You can do this anywhere in your Python code, 19 00:00:58,136 --> 00:01:01,769 but it's very commonly used when passing multiple arguments to a function. 20 00:01:01,769 --> 00:01:04,063 Your function receives the multiple arguments and 21 00:01:04,063 --> 00:01:06,590 packs them up into a single parameter. 22 00:01:06,590 --> 00:01:10,491 This parameter now references a tuple where the elements of the tuple 23 00:01:10,491 --> 00:01:14,470 are the many arguments passed to the function, kind of random right? 24 00:01:14,470 --> 00:01:18,282 Well not so much, there are definitely going to be times in your python career 25 00:01:18,282 --> 00:01:21,099 when you want to write a function that receives data, but 26 00:01:21,099 --> 00:01:24,940 you're not sure what that data will look like ahead of time. 27 00:01:24,940 --> 00:01:27,639 There's also going to be times when receiving all of your 28 00:01:27,639 --> 00:01:31,350 arguments into one tuple, cuts down on the amount of code you need to write, or 29 00:01:31,350 --> 00:01:33,280 drastically simplifies future steps. 30 00:01:33,280 --> 00:01:36,628 Let's take a look, for now just follow along with what I'm doing, 31 00:01:36,628 --> 00:01:39,061 don't worry about trying to code along with me. 32 00:01:39,061 --> 00:01:43,893 So here we have a function called packer that receives multiple arguments. 33 00:01:43,893 --> 00:01:48,050 packer prints each argument onto its own line. 34 00:01:48,050 --> 00:01:50,658 This is a pretty basic and pointless function but humor me, 35 00:01:50,658 --> 00:01:52,910 we're just using it for demonstration purposes. 36 00:01:52,910 --> 00:01:56,890 So I'm gonna write a call to this packer function where each argument is a string. 37 00:02:08,340 --> 00:02:09,939 Now I'm gonna run this, just so 38 00:02:09,939 --> 00:02:12,910 you can get a picture of what it looks like without packing. 39 00:02:17,266 --> 00:02:19,886 Each argument has been received into its own parameter and 40 00:02:19,886 --> 00:02:22,440 then it's printed out on its own line. 41 00:02:22,440 --> 00:02:25,227 Now, if we wanted to take advantage of packing in Python and 42 00:02:25,227 --> 00:02:27,235 pass these four arguments into one tuple, 43 00:02:27,235 --> 00:02:30,165 we'd change our function definition to look more like this. 44 00:02:32,531 --> 00:02:36,096 We'll get rid of each of these arguments, 45 00:02:36,096 --> 00:02:40,660 and only use one, and then we can print args on its own. 46 00:02:44,570 --> 00:02:47,050 Our function call is gonna remain exactly the same. 47 00:02:48,070 --> 00:02:52,650 Now, when we run this, we'll see that each of our four individual arguments have been 48 00:02:52,650 --> 00:02:55,990 converted to a tuple and are printed as such, let's take a look. 49 00:03:02,420 --> 00:03:04,632 So what did we do here? 50 00:03:04,632 --> 00:03:06,088 The secret is in the asterisk, 51 00:03:06,088 --> 00:03:08,720 the little star at the beginning of the parameter name. 52 00:03:10,832 --> 00:03:15,036 This star tells Python that we intend to pack all the arguments sent the function 53 00:03:15,036 --> 00:03:18,930 into whatever parameter immediately follows the star. 54 00:03:18,930 --> 00:03:22,718 This means when packer is called, and those four arguments are passed, 55 00:03:22,718 --> 00:03:26,705 the function will receive them all into one tuple stored in parameter args. 56 00:03:26,705 --> 00:03:31,547 args can then be used in the body of the function, just like any other parameter. 57 00:03:31,547 --> 00:03:35,554 And because tuples are iterable, if we still wanted each argument print on its 58 00:03:35,554 --> 00:03:38,880 own line, we can still do that, but we'll use a loop instead. 59 00:03:50,820 --> 00:03:53,080 Now I'll save, I'll come back down to my terminal. 60 00:03:53,080 --> 00:03:56,194 I'll clear this out to leave a little more room, okay, so 61 00:03:56,194 --> 00:04:00,888 when I run the file again, Cool. 62 00:04:00,888 --> 00:04:04,738 As we iterate over args each element in the tuple is printed to its own line. 63 00:04:04,738 --> 00:04:09,074 The parameter args with the asterisk before it is a very common pattern in 64 00:04:09,074 --> 00:04:10,930 Python, you'll see it a lot. 65 00:04:10,930 --> 00:04:14,780 Let's dive into args a little more in the next video with a more practical example.