1 00:00:07,860 --> 00:00:12,760 Keyword parameters are closely tied with optional parameters. 2 00:00:12,760 --> 00:00:18,760 So here, we have our function f that takes in three arguments x, y, 3 00:00:18,760 --> 00:00:26,225 and z, and y and z have default values of tree and seven. 4 00:00:26,225 --> 00:00:32,335 Now, here on line five we can see that we can call our function by only providing x, 5 00:00:32,335 --> 00:00:36,830 and then y and z get their default values. 6 00:00:38,190 --> 00:00:40,595 On line six here, 7 00:00:40,595 --> 00:00:44,025 we call our function with x equals two, 8 00:00:44,025 --> 00:00:45,855 and y equals five, 9 00:00:45,855 --> 00:00:48,285 and z getting its default value. 10 00:00:48,285 --> 00:00:52,910 But suppose we wanted to call our function and provide a value for 11 00:00:52,910 --> 00:00:58,130 x and z without actually passing in a value for y. 12 00:00:58,130 --> 00:01:02,690 So, in other words, we want to give y whatever its default value should be. 13 00:01:02,690 --> 00:01:05,300 So, if we wanted to do this, 14 00:01:05,300 --> 00:01:08,750 we would want to kind of skip a value for y. 15 00:01:08,750 --> 00:01:14,325 If we tried to do something like skipping y by saying, 16 00:01:14,325 --> 00:01:17,175 two comma blank comma eight, 17 00:01:17,175 --> 00:01:22,405 then we would get a syntax error because Python doesn't understand this format. 18 00:01:22,405 --> 00:01:26,190 Instead, what we can do is we can use a keyword parameter. 19 00:01:26,190 --> 00:01:28,560 So, rather than skipping an argument, 20 00:01:28,560 --> 00:01:33,535 we can just say that this eight is intended to be the value for z. 21 00:01:33,535 --> 00:01:38,765 We do that by saying z equals eight when we call our function. 22 00:01:38,765 --> 00:01:47,305 Now, what this does is x gets the value two and z gets the value eight. 23 00:01:47,305 --> 00:01:50,165 We never specified a value for y, 24 00:01:50,165 --> 00:01:52,745 so y gets its value three. 25 00:01:52,745 --> 00:01:54,455 When we run our code, 26 00:01:54,455 --> 00:01:59,780 we can see that in the second call here that this is the case. 27 00:01:59,780 --> 00:02:02,080 So again, x is two, 28 00:02:02,080 --> 00:02:07,740 z is eight and then y is it's default value. 29 00:02:07,740 --> 00:02:10,470 So, these are keyword arguments. 30 00:02:10,470 --> 00:02:12,240 We could also do this for x. 31 00:02:12,240 --> 00:02:16,470 So, we could say x equals two and z equals eight, 32 00:02:16,470 --> 00:02:18,795 and this would have the same outcome. 33 00:02:18,795 --> 00:02:20,950 The nice thing about keyword arguments, 34 00:02:20,950 --> 00:02:24,535 is that they allow us to put our arguments in any order. 35 00:02:24,535 --> 00:02:29,365 So here, I'll say x equals 20 and z equals eight, 36 00:02:29,365 --> 00:02:33,960 but I'll specify z before I specify x. 37 00:02:34,210 --> 00:02:37,280 Now, note that keyword arguments aren't 38 00:02:37,280 --> 00:02:39,740 going to give arguments that don't have default values, 39 00:02:39,740 --> 00:02:42,020 default values. So, for example. 40 00:02:42,020 --> 00:02:46,920 If I tried to call our function f and just specified z equals eight, 41 00:02:46,920 --> 00:02:50,270 then we haven't passed in anything for x and we get 42 00:02:50,270 --> 00:02:54,875 an error because we haven't actually specified what x is. 43 00:02:54,875 --> 00:03:00,980 So, if we wanted to do this we would have to specify a value for x somewhere or we 44 00:03:00,980 --> 00:03:07,950 would need to give x a default value in our function definition. 45 00:03:10,010 --> 00:03:13,280 So, it's also possible to 46 00:03:13,280 --> 00:03:17,075 accidentally specify multiple values for the same argument here. 47 00:03:17,075 --> 00:03:20,360 So, the easy case to see it would 48 00:03:20,360 --> 00:03:24,070 be if we specified z equals eight and then z equals four. 49 00:03:24,070 --> 00:03:29,060 If we did that, we would see that we have a keyword argument repeated online five. 50 00:03:29,060 --> 00:03:33,425 It's less easy to see if we accidentally do it by saying something like, 51 00:03:33,425 --> 00:03:38,390 passing in 10 for x by passing it in as the first argument, 52 00:03:38,390 --> 00:03:42,290 and then later on accidentally specifying that x equals eight. 53 00:03:42,290 --> 00:03:45,680 Here, we get the same kind of error. 54 00:03:45,680 --> 00:03:49,820 So, we see that we have multiple values for the argument x, 55 00:03:49,820 --> 00:03:54,690 online five and that's because here it looks 56 00:03:54,690 --> 00:04:00,005 like x should be 10 by virtue of the fact that we pass 10 in as the first argument. 57 00:04:00,005 --> 00:04:03,035 Then here, it looks like x should be eight 58 00:04:03,035 --> 00:04:06,575 by virtue of the fact that we specified x equals eight. 59 00:04:06,575 --> 00:04:09,860 Another thing to note about keyword parameters are that 60 00:04:09,860 --> 00:04:14,570 keyword parameters always have to be expressed after positional arguments. 61 00:04:14,570 --> 00:04:20,850 So, I'm going to modify our code to say that x should be 10 and z should be eight, 62 00:04:20,850 --> 00:04:22,710 and this works just fine. 63 00:04:22,710 --> 00:04:29,835 But if I change the order of this to say that z equals eight and then we pass in 10, 64 00:04:29,835 --> 00:04:32,470 then we're going to get a syntax error. 65 00:04:32,470 --> 00:04:36,470 That's because again, keyword arguments always have to come 66 00:04:36,470 --> 00:04:40,820 after any positional arguments that don't specify a keyword. 67 00:04:40,820 --> 00:04:42,230 So, in other words, 68 00:04:42,230 --> 00:04:47,940 we have to put z equals eight after specifying that x is 10. 69 00:04:47,940 --> 00:04:51,790 So, if I change the order here again, 70 00:04:52,490 --> 00:04:55,855 then we'll see that our code is fixed. 71 00:04:55,855 --> 00:05:01,049 Now, let's look at some questions that involve keyword arguments. 72 00:05:01,140 --> 00:05:03,940 So, in this question we're asked. 73 00:05:03,940 --> 00:05:06,520 What value will be printed for z? 74 00:05:06,520 --> 00:05:09,295 So, we call our function f, 75 00:05:09,295 --> 00:05:11,835 we specify x is two, 76 00:05:11,835 --> 00:05:14,770 we specify y is five, 77 00:05:14,770 --> 00:05:18,335 and then we have z equals initial. 78 00:05:18,335 --> 00:05:21,800 So, what that means is z is going to be 79 00:05:21,800 --> 00:05:25,815 the value of initial as soon as this function is declared. 80 00:05:25,815 --> 00:05:28,825 We can see that initial is set here, 81 00:05:28,825 --> 00:05:33,000 and so the value of z is going to be seven. 82 00:05:35,360 --> 00:05:37,590 In this question we're asked. 83 00:05:37,590 --> 00:05:40,110 What value will be printed for y? 84 00:05:40,110 --> 00:05:42,800 So, we have the same function definition. 85 00:05:42,800 --> 00:05:46,190 So, we specify initial equals seven and then 86 00:05:46,190 --> 00:05:50,725 we provide default values for y which is going to be three, 87 00:05:50,725 --> 00:05:53,220 and for z which is going to be seven. 88 00:05:53,220 --> 00:05:56,010 Then, when we call our function f, 89 00:05:56,010 --> 00:05:58,605 we pass in a value for x, 90 00:05:58,605 --> 00:06:03,135 which is two and a value for z, which is 10. 91 00:06:03,135 --> 00:06:07,850 So, what that means, is that y gets its default value, which is three. 92 00:06:07,850 --> 00:06:11,610 So, we should get three printed out for y. 93 00:06:12,770 --> 00:06:15,105 In this question we're asked. 94 00:06:15,105 --> 00:06:17,805 What value will be printed for x? 95 00:06:17,805 --> 00:06:23,175 So, we have the same function definition x, y, and z. 96 00:06:23,175 --> 00:06:25,320 When we call our function f, 97 00:06:25,320 --> 00:06:29,265 we specify that X is two here. 98 00:06:29,265 --> 00:06:34,275 But, then we also specified that x is five, 99 00:06:34,275 --> 00:06:36,220 and this isn't going to fly. 100 00:06:36,220 --> 00:06:39,710 Python is going to give us a runtime error because we tried to 101 00:06:39,710 --> 00:06:44,045 specify two different values for x, two and five. 102 00:06:44,045 --> 00:06:49,785 So, the answer here is E. In this question we're asked. 103 00:06:49,785 --> 00:06:52,530 What value will be printed for z? 104 00:06:52,530 --> 00:07:00,170 So, we have the same function definition as before arguments x, y, and z. 105 00:07:00,170 --> 00:07:05,330 We specify that z's default value is going to be initial. 106 00:07:05,330 --> 00:07:07,400 When we define our function, 107 00:07:07,400 --> 00:07:10,040 the value of initial is seven, 108 00:07:10,040 --> 00:07:16,110 but it just so happens that we overwrite that value later on so that it's zero. 109 00:07:16,110 --> 00:07:22,130 But again, these default values are only evaluated when we declare the function. 110 00:07:22,130 --> 00:07:26,090 When we declare this function f initial had the value seven, 111 00:07:26,090 --> 00:07:30,990 and so we can just almost replaced this with the value seven. 112 00:07:30,990 --> 00:07:34,250 It doesn't matter that we changed initial later on, 113 00:07:34,250 --> 00:07:37,085 z is going to have the value seven by default. 114 00:07:37,085 --> 00:07:40,080 So, the answer here is B. 115 00:07:41,210 --> 00:07:45,070 That's all for now, until next time.