1 00:00:00,000 --> 00:00:02,130 Let's talk about 'args' and 'kwargs'. 2 00:00:02,130 --> 00:00:07,800 'args' are arguments, and 'kwargs' are key word arguments. 3 00:00:07,800 --> 00:00:10,700 Keyword is one word I think, keyword arguments. 4 00:00:11,700 --> 00:00:15,200 So what you're going to see in the while, in other people's 5 00:00:15,200 --> 00:00:19,000 code is something like, 'def', you've got a 'func_name' in here, 6 00:00:19,000 --> 00:00:20,000 some function name, 7 00:00:20,000 --> 00:00:21,800 it's going to take some sort of parameter, 8 00:00:22,800 --> 00:00:27,200 let's say it takes a 'name', and then has '*args' and then '**kwargs'. 9 00:00:28,200 --> 00:00:32,100 Now, '*args' can technically be named anything, but it is denoted 10 00:00:32,180 --> 00:00:34,710 by a single asterisk. 11 00:00:35,430 --> 00:00:38,310 This means that there are going to be other arguments in 12 00:00:38,320 --> 00:00:40,320 this function. And keyword arguments, 13 00:00:40,330 --> 00:00:43,350 again, this could be called anything else, but by convention, 14 00:00:43,360 --> 00:00:44,730 we call this 'kwargs', 15 00:00:44,730 --> 00:00:46,400 and this one is 'args'. 16 00:00:46,400 --> 00:00:50,000 This one is denoted with two asterisks, '**' before it. 17 00:00:51,200 --> 00:00:53,700 So let's go ahead and explore some 'args' first. 18 00:00:53,700 --> 00:00:57,100 So let's go ahead and just say '*args', 19 00:00:57,600 --> 00:01:00,500 and in our function, we're simply going to loop through these 'args'. 20 00:01:00,500 --> 00:01:03,900 But first, I want to actually print out what 'args' is. 21 00:01:03,940 --> 00:01:07,450 'print(args)', and notice that there is asterisk in here. 22 00:01:07,460 --> 00:01:10,030 This is simply saying that, "Hey, there's going to be other 23 00:01:10,030 --> 00:01:13,300 arguments in here, and use 'args' as the variable name in here. 24 00:01:13,300 --> 00:01:14,700 So 'print(args)'. 25 00:01:14,700 --> 00:01:17,700 Let's also 'print(type(args))'. 26 00:01:17,700 --> 00:01:19,100 What is this going to be? 27 00:01:19,900 --> 00:01:22,300 And let's run 'def func_name' here. 28 00:01:22,900 --> 00:01:26,500 '(1, 5, 8, 'Python', Coding')'. I don't know. 29 00:01:26,500 --> 00:01:28,440 I'm just filling with some random stuff in here. 30 00:01:28,450 --> 00:01:30,360 And we can see when we print the arguments 31 00:01:30,520 --> 00:01:32,230 this is a tuple. 32 00:01:32,900 --> 00:01:36,400 And so the 'args' are a tuple, means they cannot be changed. 33 00:01:36,470 --> 00:01:39,040 We can't add more values to the arguments. 34 00:01:39,040 --> 00:01:42,400 We simply cannot do that because tuples don't allow us to do that. 35 00:01:42,400 --> 00:01:45,100 They're immutable. Which means we're stuck with the 36 00:01:45,100 --> 00:01:46,200 values that come in here. 37 00:01:46,200 --> 00:01:47,400 Doesn't mean we have to use them, 38 00:01:47,600 --> 00:01:51,620 but we cannot change 'args' as a variable name inside of this 39 00:01:51,630 --> 00:01:53,480 function. Once it is set, it is set in stone. 40 00:01:53,600 --> 00:01:57,650 So let's see how we can access these arguments here. 41 00:01:57,700 --> 00:02:00,600 'def', we're going to call this one 'print_args', and it's going 42 00:02:00,600 --> 00:02:02,500 to take any number of arguments, 43 00:02:02,500 --> 00:02:05,000 and it could be literally any number of arguments. 44 00:02:05,000 --> 00:02:06,300 It could be 1, it could be 0, 45 00:02:06,300 --> 00:02:11,000 it could be a 1000. 'for arg in args', we could simply print 46 00:02:11,000 --> 00:02:13,699 the argument value, whatever that is going to be. 47 00:02:15,400 --> 00:02:23,900 And so if we say 'print_args('Computer', 'Coffee', 'Cup', 'Monitor', 48 00:02:23,960 --> 00:02:28,550 'Lamp')', these are literally just things that I can see 49 00:02:28,550 --> 00:02:29,750 on my desk right now. 50 00:02:30,020 --> 00:02:32,570 This function is going to go through all of these arguments, 51 00:02:32,900 --> 00:02:34,800 and just going to print them out for us. 52 00:02:34,800 --> 00:02:37,800 Go through each 'arg' one by 1. 53 00:02:37,860 --> 00:02:39,840 Now that is a standard argument, 54 00:02:39,840 --> 00:02:42,400 but we also have these things called 'kwargs', 55 00:02:42,480 --> 00:02:43,410 keyword arguments. 56 00:02:43,410 --> 00:02:45,870 Keyword arguments are like what we saw in the last video 57 00:02:45,870 --> 00:02:49,600 where the 'role' was 'Student' by default. 58 00:02:49,620 --> 00:02:51,480 Let's go ahead and create an example here. 59 00:02:51,480 --> 00:02:57,030 'def my_func', and it's going to take not 'args', but just 'kwargs', 60 00:02:57,030 --> 00:03:02,100 so '**kwargs', and let's print out what 'kwargs' is going to be, 61 00:03:02,160 --> 00:03:04,510 and let's also print out the 'type(kwargs)', 62 00:03:04,510 --> 00:03:05,900 so we know what we're working with. 63 00:03:05,900 --> 00:03:08,900 [no audio] 64 00:03:08,900 --> 00:03:11,600 and 'my_func', and 65 00:03:11,660 --> 00:03:13,300 this is going to take keyword argument, 66 00:03:13,310 --> 00:03:14,230 that's what 'kwargs' is, 67 00:03:14,300 --> 00:03:19,300 keyword arguments. 'name = 'Kalob', 'drink' = 'Coffee', is equal 68 00:03:19,370 --> 00:03:24,920 'hobby =', not 'Planting', 'Gardening', something like 69 00:03:24,930 --> 00:03:28,710 that, and that is not going to work because I typoed the 70 00:03:28,710 --> 00:03:30,200 function name. There we go. 71 00:03:30,220 --> 00:03:33,290 So this came back as a dictionary. 'print(type( 72 00:03:33,300 --> 00:03:37,000 kwargs))', came back as a dictionary, and we get a key:value, 73 00:03:37,000 --> 00:03:39,400 key:value, key:value. So that's interesting. 74 00:03:39,450 --> 00:03:41,420 Now we have something to work with. 75 00:03:41,430 --> 00:03:43,790 We know about tuples, we know about dictionaries, 76 00:03:43,800 --> 00:03:45,300 we have something to work with here. 77 00:03:45,380 --> 00:03:48,620 So let's go ahead and take this exact same example, 78 00:03:48,620 --> 00:03:53,000 but let's loop through every key:value, and just print it back. 79 00:03:53,000 --> 00:03:57,600 So we could say, 'for key, value in kwargs.items'. 80 00:03:57,600 --> 00:03:59,800 We know it's a dictionary, and we can use '.items' on it. 81 00:03:59,940 --> 00:04:04,230 'print("Key:', is going to be whatever the 'key' is going to be, 82 00:04:04,400 --> 00:04:06,900 let's go ahead put an arrow in here or not an arrow, 83 00:04:06,900 --> 00:04:11,100 let's do a few tabs, and then the "Value:" is going to be whatever 84 00:04:11,100 --> 00:04:12,100 the 'value' is going to be. 85 00:04:12,100 --> 00:04:16,700 And let's take this exact example again, and we can see 'key' 86 00:04:16,700 --> 00:04:20,500 is equal to 'name', the 'value' is 'Kalob'; 'drink', 'Coffee'; 'hobby', 'Gardening'. 87 00:04:20,500 --> 00:04:22,899 Now, at this point in time, this is actually super useless 88 00:04:22,990 --> 00:04:26,110 because you're never really going to do this in real Python 89 00:04:26,120 --> 00:04:30,050 programming. But let's say you're going to order food from 90 00:04:30,050 --> 00:04:32,800 a food ordering company. I don't know, 91 00:04:32,810 --> 00:04:36,120 "Uber Eats", "SkipTheDishes", "DoorDash", whatever company, 92 00:04:36,130 --> 00:04:38,520 they're going to have some sort of function or a series of 93 00:04:38,520 --> 00:04:40,300 functions that's going to accept an order. 94 00:04:40,320 --> 00:04:43,840 So let's create an 'order()' function in here, and it needs to 95 00:04:43,850 --> 00:04:46,540 take your 'name' because they need to know who to deliver to. 96 00:04:46,600 --> 00:04:49,960 We could also say, it needs to take your address, but that's 97 00:04:49,970 --> 00:04:52,780 getting a little too complicated, a little too much information, 98 00:04:52,790 --> 00:04:54,250 you'll just watch me type for too long. 99 00:04:54,260 --> 00:04:57,140 So it's going to take someone's 'name', and maybe some other notes, 100 00:04:57,150 --> 00:05:00,950 some other keyword 'args', or just 'args', not keyword 'args', 101 00:05:00,950 --> 00:05:03,000 and then it's also going to take some 'kwargs'. 102 00:05:03,080 --> 00:05:06,800 And this is going to say 'print("Hello", )'. 103 00:05:06,810 --> 00:05:07,820 This is an f-string. 104 00:05:07,830 --> 00:05:10,880 So let's go ahead and throw an 'f"' at the beginning of that. 105 00:05:10,880 --> 00:05:15,300 The other 'args' in here could be different dishes that you want. 106 00:05:15,300 --> 00:05:20,100 So we could say 'for arg in args: print( 107 00:05:21,200 --> 00:05:28,000 "\t You ordered, You ordered an ", and that's an f-string as well. 108 00:05:29,800 --> 00:05:32,000 Now, to make this a little simpler, we could actually change this 109 00:05:32,000 --> 00:05:33,900 from 'args' to 'dishes', 110 00:05:33,900 --> 00:05:36,000 [no audio] 111 00:05:36,000 --> 00:05:40,500 and let's call this 'dish' and "You ordered a ". 112 00:05:41,700 --> 00:05:44,200 And we could also check to see if someone's address, 113 00:05:44,200 --> 00:05:46,700 this is a good example, if someone's address is in the keyword 114 00:05:46,700 --> 00:05:51,100 'args', so we can say 'if kwargs.get("address") 115 00:05:51,100 --> 00:05:52,900 print("\t 116 00:05:53,800 --> 00:05:57,300 We are delivering to )'. 117 00:05:57,300 --> 00:06:00,000 And let's reassign this. 118 00:06:00,000 --> 00:06:01,160 That's also an f-string, 119 00:06:01,160 --> 00:06:03,800 so boop little 'f"' there. 120 00:06:04,400 --> 00:06:11,300 And let's create 'address = kwargs.get('address')'. 121 00:06:11,380 --> 00:06:14,200 And this is only going to show up if address is actually 122 00:06:14,200 --> 00:06:15,800 in one of the keyword 'args'. 123 00:06:15,800 --> 00:06:18,300 So let's hit 'Enter' there, and let's create an 'order'. 124 00:06:18,900 --> 00:06:23,600 An 'order' is going to be 'for', "Zephyr", 125 00:06:23,600 --> 00:06:27,700 he's going to order "tacos" and also "cat food", because he's 126 00:06:27,700 --> 00:06:31,300 a cat, and his address is a keyword argument. 127 00:06:31,500 --> 00:06:35,500 His address is going to be, "123 Meow Lane" 128 00:06:35,900 --> 00:06:37,600 And look at that it says, "Hello Zephyr 129 00:06:37,690 --> 00:06:40,650 "You ordered tacos". "You ordered an tacos". 130 00:06:40,660 --> 00:06:44,500 "You ordered tacos". "You ordered cat food". And, "We are going to 131 00:06:44,500 --> 00:06:46,800 deliver to 123 Meow Lane". 132 00:06:46,860 --> 00:06:49,230 Let's go ahead and rerun this without an 'address'. 133 00:06:49,240 --> 00:06:50,160 Let's see what happens. 134 00:06:50,180 --> 00:06:53,660 And let's also get rid of 'an', because that's just bad grammar. 135 00:06:54,340 --> 00:06:57,440 There were no keyword arguments provided. 136 00:06:57,440 --> 00:07:01,900 And 'address', where we saw 'address =', does not exist. 137 00:07:01,910 --> 00:07:05,270 So it's skipped over this because it didn't exist. 138 00:07:05,270 --> 00:07:07,300 So the 'it' statement was never triggered. 139 00:07:07,900 --> 00:07:10,500 In fact, we can actually do something a little more explicit 140 00:07:10,560 --> 00:07:15,860 here and say, 'print("You', all caps, let's yell at the person 141 00:07:15,860 --> 00:07:20,100 "YOU DID NOT PROVIDE AN ADDRESS WOW 142 00:07:20,100 --> 00:07:23,000 CMON")'. Let's rerun that one. 143 00:07:23,900 --> 00:07:24,900 Let's rerun that one. 144 00:07:24,900 --> 00:07:27,000 It says, "YOU DID NOT PROVIDE AN ADDRESS WOW" 145 00:07:27,800 --> 00:07:28,900 'address', 146 00:07:28,900 --> 00:07:31,600 and this could be anything at this point, anything. 147 00:07:31,600 --> 00:07:33,800 [no audio] 148 00:07:33,800 --> 00:07:36,300 So this is a little bit of an example, a fairly good example 149 00:07:36,340 --> 00:07:38,430 that I think a lot of people can relate to these days, 150 00:07:38,430 --> 00:07:39,600 where your ordering food, 151 00:07:39,680 --> 00:07:42,530 you need a name because your account has a name, 152 00:07:42,530 --> 00:07:45,170 they need to know what dishes you're ordering, which could 153 00:07:45,180 --> 00:07:48,720 be any number of arguments and also keyword arguments, 154 00:07:48,730 --> 00:07:52,770 things like your address or any special delivery note could 155 00:07:52,770 --> 00:07:54,000 be a keyword argument. 156 00:07:54,040 --> 00:07:55,630 So why is this important? 157 00:07:55,640 --> 00:07:59,950 Because in the wild, especially in web frameworks like Django 158 00:08:00,220 --> 00:08:03,670 or Flask, you're going to see a lot of function parameters 159 00:08:03,680 --> 00:08:05,170 where you just have a regular argument, 160 00:08:05,360 --> 00:08:09,110 and then you've got unlimited arguments, and then you have 161 00:08:09,120 --> 00:08:12,300 keyword args, and you're going to see these working together 162 00:08:12,300 --> 00:08:14,000 all the time.