WEBVTT 1 00:00:00.740 --> 00:00:02.520 line:15% Hi guys, and welcome back. 2 00:00:02.520 --> 00:00:04.180 line:15% In this video, we're going to learn about 3 00:00:04.180 --> 00:00:07.560 default values for parameters. 4 00:00:07.560 --> 00:00:10.140 Let's say you've got a function called add, 5 00:00:10.140 --> 00:00:12.570 and it digs through numbers x and y, 6 00:00:12.570 --> 00:00:16.130 and calculates a total, and then returns it. 7 00:00:16.130 --> 00:00:17.950 Then, let's say we call it with 8 00:00:17.950 --> 00:00:20.460 the values five and 10, and print it out. 9 00:00:20.460 --> 00:00:22.960 So, if we run this, you get 15 10 00:00:22.960 --> 00:00:26.200 because five gets assigned to x, 11 00:00:26.200 --> 00:00:27.640 10 gets assigned to y, 12 00:00:27.640 --> 00:00:31.860 and then the sum, 15, gets assigned to the variable total. 13 00:00:31.860 --> 00:00:34.630 The value of the variable is returned, 14 00:00:34.630 --> 00:00:37.500 and then that value is printed out. 15 00:00:37.500 --> 00:00:41.540 In Python, you can say, y equal three, for example, 16 00:00:41.540 --> 00:00:42.900 and what that means is that 17 00:00:42.900 --> 00:00:46.560 if you don't pass in this second value, 18 00:00:46.560 --> 00:00:48.604 then it will be three by default. 19 00:00:48.604 --> 00:00:51.670 So, you can call this function with a single value 20 00:00:51.670 --> 00:00:52.650 if you prefer. 21 00:00:52.650 --> 00:00:53.830 So, you can say, add 22 00:00:53.830 --> 00:00:56.450 and just pass in five inside the brackets 23 00:00:56.450 --> 00:00:59.010 and then y, because it doesn't have a value, 24 00:00:59.010 --> 00:01:01.370 will take the value of three. 25 00:01:01.370 --> 00:01:03.640 So, if we run this, you'll get eight. 26 00:01:03.640 --> 00:01:06.654 Notice that you can still pass in a value if you want, 27 00:01:06.654 --> 00:01:09.540 and that just means that y will be this value, 28 00:01:09.540 --> 00:01:12.700 and it will not take the default value. 29 00:01:12.700 --> 00:01:14.470 Also, let's take this opportunity to talk 30 00:01:14.470 --> 00:01:17.100 about named arguments. 31 00:01:17.100 --> 00:01:20.900 In Python, to make calling the function a bit more readable, 32 00:01:20.900 --> 00:01:23.130 you can say, x equal three, 33 00:01:23.130 --> 00:01:25.480 when you're calling the function. 34 00:01:25.480 --> 00:01:29.460 And what this says is x, this parameter here, 35 00:01:29.460 --> 00:01:31.023 will take the value three. 36 00:01:32.320 --> 00:01:33.970 Y will still take the value three 37 00:01:33.970 --> 00:01:36.040 because that's the default value. 38 00:01:36.040 --> 00:01:40.020 If you run this, you'll see that the value is six. 39 00:01:40.020 --> 00:01:41.190 You can say something like, 40 00:01:41.190 --> 00:01:43.440 x equal five and y equals two, 41 00:01:43.440 --> 00:01:45.000 and that's also totally fine. 42 00:01:45.000 --> 00:01:47.130 Now the result will be seven. 43 00:01:47.130 --> 00:01:50.130 And again, what this says is the parameter x 44 00:01:50.130 --> 00:01:51.010 has the value of five, 45 00:01:51.010 --> 00:01:53.293 the parameter y will take the value two. 46 00:01:54.140 --> 00:01:57.490 Notice that this, here, is something we're doing 47 00:01:57.490 --> 00:01:59.090 when we call the function. 48 00:01:59.090 --> 00:02:01.190 So, when we use it. 49 00:02:01.190 --> 00:02:03.070 And this, here, is something we do 50 00:02:03.070 --> 00:02:04.843 when we define the function. 51 00:02:05.800 --> 00:02:08.480 These are called named arguments, 52 00:02:08.480 --> 00:02:11.070 and they're there so that when you call a function, 53 00:02:11.070 --> 00:02:14.320 it's clearer what each number is for. 54 00:02:14.320 --> 00:02:16.370 So, here the five is for x, 55 00:02:16.370 --> 00:02:18.130 the two is for y. 56 00:02:18.130 --> 00:02:20.010 But when you define a function, 57 00:02:20.010 --> 00:02:23.270 y equal three means a default parameter. 58 00:02:23.270 --> 00:02:26.980 So, if you don't pass a value, y will be three. 59 00:02:26.980 --> 00:02:28.450 Even though the syntax are the same, 60 00:02:28.450 --> 00:02:30.200 y equal three and y equal two, 61 00:02:30.200 --> 00:02:32.040 they do mean slightly different things 62 00:02:32.040 --> 00:02:34.580 depending on where you use them. 63 00:02:34.580 --> 00:02:37.580 Something important to remember is that 64 00:02:37.580 --> 00:02:39.653 you cannot do this. 65 00:02:40.680 --> 00:02:45.200 If you do x equal five comma two, 66 00:02:45.200 --> 00:02:46.400 you'll get an error. 67 00:02:46.400 --> 00:02:50.350 And that's because the argument that doesn't have a name 68 00:02:50.350 --> 00:02:55.010 cannot be used after an argument that has a name. 69 00:02:55.010 --> 00:02:59.270 So, if the first argument has a name, such as x, 70 00:02:59.270 --> 00:03:03.320 then all subsequent arguments also must have a name. 71 00:03:03.320 --> 00:03:05.193 Otherwise it will give you the error. 72 00:03:06.360 --> 00:03:07.810 The opposite can be done, though. 73 00:03:07.810 --> 00:03:10.020 The first argument can not have a name 74 00:03:10.020 --> 00:03:12.610 and the second one can have a name, and this is fine. 75 00:03:12.610 --> 00:03:15.100 So, it just means that if the first one has a name, 76 00:03:15.100 --> 00:03:17.080 the second one needs to have a name, too. 77 00:03:17.080 --> 00:03:19.110 But if the first one doesn't have a name, 78 00:03:19.110 --> 00:03:20.930 the second one can still have one. 79 00:03:20.930 --> 00:03:23.743 So, that applies for all the arguments. 80 00:03:24.720 --> 00:03:27.360 Similarly, if you just do y equal two, 81 00:03:27.360 --> 00:03:29.600 Python knows that you want the value two 82 00:03:29.600 --> 00:03:32.143 to go to the parameter y. 83 00:03:33.340 --> 00:03:36.080 So, instead of three, which is the default value, 84 00:03:36.080 --> 00:03:37.770 we will use two. 85 00:03:37.770 --> 00:03:38.990 That's all well and good, 86 00:03:38.990 --> 00:03:41.070 but we don't have a value for x. 87 00:03:41.070 --> 00:03:43.420 So, Python will give you an error telling you that 88 00:03:43.420 --> 00:03:47.850 you are missing one required positional argument. 89 00:03:47.850 --> 00:03:50.230 A positional argument is just an argument 90 00:03:50.230 --> 00:03:51.300 that doesn't have a name. 91 00:03:51.300 --> 00:03:53.270 So, here, we have a named argument, 92 00:03:53.270 --> 00:03:55.690 also called keyword argument, 93 00:03:55.690 --> 00:03:58.580 and the x is a positional argument 94 00:03:58.580 --> 00:04:00.720 if we do something, like, five. 95 00:04:00.720 --> 00:04:03.660 So, you do need to provide a value for x 96 00:04:03.660 --> 00:04:06.940 because it doesn't have a default value. 97 00:04:06.940 --> 00:04:08.220 The same thing applies when 98 00:04:08.220 --> 00:04:09.620 defining a function, by the way. 99 00:04:09.620 --> 00:04:12.380 So, you cannot have x equal five comma y 100 00:04:12.380 --> 00:04:14.610 because you've got a default value here, 101 00:04:14.610 --> 00:04:16.120 but not here. 102 00:04:16.120 --> 00:04:18.400 Default values must go at the end. 103 00:04:18.400 --> 00:04:20.940 So, if one parameter has a default value, 104 00:04:20.940 --> 00:04:25.020 all subsequent parameters must have default values as well. 105 00:04:25.020 --> 00:04:26.960 So, this would also give you an error 106 00:04:26.960 --> 00:04:27.867 because it says, 107 00:04:27.867 --> 00:04:30.827 "non-default argument follows default argument." 108 00:04:32.250 --> 00:04:35.040 Many functions in Python have these named, 109 00:04:35.040 --> 00:04:37.140 or keyword, arguments. 110 00:04:37.140 --> 00:04:39.250 For example, the print function. 111 00:04:39.250 --> 00:04:42.980 Say you print one, two, three, four, and five. 112 00:04:42.980 --> 00:04:45.520 What we're doing here is we're passing the value one 113 00:04:45.520 --> 00:04:46.850 to the print function, 114 00:04:46.850 --> 00:04:48.880 and then we're passing the value two to the print function, 115 00:04:48.880 --> 00:04:50.810 and then three, and then four, and then five. 116 00:04:50.810 --> 00:04:53.050 So, we're passing five different arguments 117 00:04:53.050 --> 00:04:54.160 to the print function. 118 00:04:54.160 --> 00:04:56.620 If we run this, you'll see that you get them printed out 119 00:04:56.620 --> 00:04:58.423 with a space in between each. 120 00:04:59.390 --> 00:05:03.750 But you can actually pass a special argument called sep, 121 00:05:03.750 --> 00:05:06.910 and make it equal to whatever string you want. 122 00:05:06.910 --> 00:05:08.300 And then, instead of the space, 123 00:05:08.300 --> 00:05:10.330 it will use that separator. 124 00:05:10.330 --> 00:05:12.320 That's what sep stands for. 125 00:05:12.320 --> 00:05:15.450 So, here's an example of one of the built-in functions, 126 00:05:15.450 --> 00:05:18.940 print, that has one of these named arguments 127 00:05:18.940 --> 00:05:20.852 that you can use as well. 128 00:05:20.852 --> 00:05:24.070 If you have a variable such as default y, 129 00:05:24.070 --> 00:05:26.420 and you want to create a function that uses it 130 00:05:26.420 --> 00:05:29.460 as one of its default values, 131 00:05:29.460 --> 00:05:32.280 this is highly discouraged. 132 00:05:32.280 --> 00:05:35.844 Here's this function which has a parameter x 133 00:05:35.844 --> 00:05:39.490 and a parameter y with a default prop value of 134 00:05:39.490 --> 00:05:42.150 the value of default y. 135 00:05:42.150 --> 00:05:45.810 What this does is when Python reaches line three, 136 00:05:45.810 --> 00:05:47.530 and it defines the function, 137 00:05:47.530 --> 00:05:50.290 it remembers the name of the function, add, 138 00:05:50.290 --> 00:05:53.820 and then it also calculates the default value 139 00:05:53.820 --> 00:05:56.700 which is, at this point in time, three. 140 00:05:56.700 --> 00:05:59.010 So, if you do add two, 141 00:05:59.010 --> 00:06:01.420 that means that x will take the value two, 142 00:06:01.420 --> 00:06:03.890 and y will take the value three. 143 00:06:03.890 --> 00:06:07.329 If we run this, you'll see that five is printed out. 144 00:06:07.329 --> 00:06:11.429 But if you now change default y to, say, four, 145 00:06:11.429 --> 00:06:14.733 and then you add two again, 146 00:06:15.660 --> 00:06:18.240 you'll see that five gets printed out again. 147 00:06:18.240 --> 00:06:19.990 So, what this tells us is that 148 00:06:19.990 --> 00:06:22.403 when Python defines the function, 149 00:06:23.270 --> 00:06:26.693 it also stores the default value at the time. 150 00:06:27.630 --> 00:06:29.960 So, even if you change the variable 151 00:06:29.960 --> 00:06:32.010 that is the default value, 152 00:06:32.010 --> 00:06:34.600 it will not change when you use the function. 153 00:06:34.600 --> 00:06:37.440 It still keeps the value that was used 154 00:06:37.440 --> 00:06:40.230 when you defined the function initially. 155 00:06:40.230 --> 00:06:42.730 Therefore, it is highly discouraged to use 156 00:06:42.730 --> 00:06:46.810 variables as default values because of this thing. 157 00:06:46.810 --> 00:06:48.100 The fact that you use the variable 158 00:06:48.100 --> 00:06:50.550 doesn't mean that this value is going to change 159 00:06:50.550 --> 00:06:52.765 whenever you change the variable. 160 00:06:52.765 --> 00:06:54.370 I'll say at this point, 161 00:06:54.370 --> 00:06:57.100 be careful when using lists or dictionaries 162 00:06:57.100 --> 00:06:59.430 as default parameter values. 163 00:06:59.430 --> 00:07:01.670 That's because unlike strings and integers, 164 00:07:01.670 --> 00:07:05.680 these will update if you change them inside the functions. 165 00:07:05.680 --> 00:07:08.820 And this is due to a language feature called mutability. 166 00:07:08.820 --> 00:07:11.160 It's not important for you to understand this now, 167 00:07:11.160 --> 00:07:14.050 but just bear in mind that lists and dictionaries 168 00:07:14.050 --> 00:07:16.630 behave differently than strings and integers 169 00:07:16.630 --> 00:07:18.000 behind the scenes. 170 00:07:18.000 --> 00:07:18.958 That's it for this video. 171 00:07:18.958 --> 00:07:20.510 Hope you've learned something 172 00:07:20.510 --> 00:07:22.160 line:15% and I'll see you in the next one.