WEBVTT 1 00:00:00.680 --> 00:00:02.320 Hi guys and welcome back. 2 00:00:02.320 --> 00:00:05.451 In this video, we'll be talking about lambda functions. 3 00:00:05.451 --> 00:00:09.120 Lambda functions are used to get inputs, 4 00:00:09.120 --> 00:00:11.200 do a small amount of processing, 5 00:00:11.200 --> 00:00:13.130 and return outputs. 6 00:00:13.130 --> 00:00:14.810 Functions can do two things. 7 00:00:14.810 --> 00:00:16.640 They can perform an action, 8 00:00:16.640 --> 00:00:18.790 or they can return an output, 9 00:00:18.790 --> 00:00:20.410 or they can do both. 10 00:00:20.410 --> 00:00:22.700 For example, the print function, 11 00:00:22.700 --> 00:00:24.250 is one that performs an action. 12 00:00:24.250 --> 00:00:26.090 It shows something to the user. 13 00:00:26.090 --> 00:00:29.130 But it doesn't really modify the data it receives, 14 00:00:29.130 --> 00:00:31.290 and gives you some new data. 15 00:00:31.290 --> 00:00:34.092 However if you have a function, such as this divide function 16 00:00:34.092 --> 00:00:36.580 which takes two numbers and processes them, 17 00:00:36.580 --> 00:00:38.170 and returns some output, 18 00:00:38.170 --> 00:00:42.080 this could be a good function to use in a lambda function. 19 00:00:42.080 --> 00:00:44.400 The print function, though, would not be. 20 00:00:44.400 --> 00:00:46.710 That is because lambda functions are used 21 00:00:46.710 --> 00:00:49.300 to take inputs and return outputs. 22 00:00:49.300 --> 00:00:51.310 If you wanted to rewrite this function, 23 00:00:51.310 --> 00:00:53.680 which at the moment is a normal function, 24 00:00:53.680 --> 00:00:56.853 into a lambda function, you can do this. 25 00:00:57.775 --> 00:00:59.858 (typing) 26 00:01:04.210 --> 00:01:07.030 Here we are creating the lambda function, 27 00:01:07.030 --> 00:01:10.850 and we are assigning it to the divide variable. 28 00:01:10.850 --> 00:01:13.323 If we do this, we no longer need this. 29 00:01:14.645 --> 00:01:17.410 The structure of the lambda function is as follows. 30 00:01:17.410 --> 00:01:18.900 And I appreciate it is bit confusing 31 00:01:18.900 --> 00:01:20.820 because of it's short nature, 32 00:01:20.820 --> 00:01:22.840 and the amount of symbols involved. 33 00:01:22.840 --> 00:01:25.630 You've got the lambda keyword, 34 00:01:25.630 --> 00:01:27.470 then you've got a space, 35 00:01:27.470 --> 00:01:30.150 then you've got a list of parameters. 36 00:01:30.150 --> 00:01:32.640 So these are the inputs to the function. 37 00:01:32.640 --> 00:01:34.660 Here we've got two different inputs. 38 00:01:34.660 --> 00:01:37.580 One called X and one called Y. 39 00:01:37.580 --> 00:01:41.670 Then after a colon, you have the return value, 40 00:01:41.670 --> 00:01:42.790 of the function. 41 00:01:42.790 --> 00:01:45.760 So this function here takes two inputs, 42 00:01:45.760 --> 00:01:48.450 and returns the division of the two. 43 00:01:48.450 --> 00:01:51.960 So again, it is identical to this function here, 44 00:01:51.960 --> 00:01:53.960 because this one also takes two inputs 45 00:01:53.960 --> 00:01:55.570 and returns their division. 46 00:01:55.570 --> 00:01:57.500 This one also returns the division 47 00:01:57.500 --> 00:02:00.620 even though it doesn't have explicit return statement. 48 00:02:00.620 --> 00:02:02.960 This is what lambda functions do. 49 00:02:02.960 --> 00:02:06.240 So, because it's got the divide name, 50 00:02:06.240 --> 00:02:11.240 you can still call it as you would any other function. 51 00:02:11.470 --> 00:02:13.877 Just call it with the brackets and put the values in there, 52 00:02:13.877 --> 00:02:15.900 and you get 5.0. 53 00:02:15.900 --> 00:02:18.180 Now, something interesting about lambda functions, 54 00:02:18.180 --> 00:02:22.480 is what happens if you don't assign a variable to them? 55 00:02:22.480 --> 00:02:26.010 Well, python will create this function, 56 00:02:26.010 --> 00:02:27.810 and immediate destroy it. 57 00:02:27.810 --> 00:02:30.270 If there is no name associated with this function, 58 00:02:30.270 --> 00:02:33.432 python realises that you know, you can't use this function, 59 00:02:33.432 --> 00:02:36.330 because you have no way of referring to it. 60 00:02:36.330 --> 00:02:38.390 So it will immediately destroy it. 61 00:02:38.390 --> 00:02:43.390 However, you can call this function like this. 62 00:02:44.980 --> 00:02:49.120 This is even more confusing and I would almost never do this 63 00:02:49.120 --> 00:02:51.990 but here you use brackets to tell python, 64 00:02:51.990 --> 00:02:54.270 you want to run this code first. 65 00:02:54.270 --> 00:02:56.090 You create the function. 66 00:02:56.090 --> 00:02:58.930 And now that you have this function, 67 00:02:58.930 --> 00:03:01.430 you can use the bracket notation to call it, 68 00:03:01.430 --> 00:03:03.053 and pass in two values. 69 00:03:04.140 --> 00:03:08.700 Remember that this is identical to doing this, 70 00:03:08.700 --> 00:03:12.030 if the divide function was already defined. 71 00:03:12.030 --> 00:03:13.280 But because it's not, 72 00:03:13.280 --> 00:03:15.470 you can do this and it defines the function, 73 00:03:15.470 --> 00:03:18.820 and then the brackets signify that you want to call it, 74 00:03:18.820 --> 00:03:21.070 and give it two values. 75 00:03:21.070 --> 00:03:22.600 Now notice that if you run this, 76 00:03:22.600 --> 00:03:26.100 the function does evaluate, but nothing gets printed out. 77 00:03:26.100 --> 00:03:29.050 So you have to check this into a printstatement. 78 00:03:29.050 --> 00:03:31.910 Which makes it even more confusing. 79 00:03:31.910 --> 00:03:34.520 Now you can get 5.0 back but this code 80 00:03:34.520 --> 00:03:38.510 is pretty crap code because there are a lot of brackets, 81 00:03:38.510 --> 00:03:41.090 symbols, things going on in a single line, 82 00:03:41.090 --> 00:03:43.830 that frankly don't have to be in one line. 83 00:03:43.830 --> 00:03:45.870 You could define this in another line, 84 00:03:45.870 --> 00:03:47.350 and make this a bit more readable, 85 00:03:47.350 --> 00:03:49.530 make it a little bit clearer of what's going on. 86 00:03:49.530 --> 00:03:52.330 So what is the purpose of lambda functions? 87 00:03:52.330 --> 00:03:55.090 Lambda functions can at times, 88 00:03:55.090 --> 00:03:57.640 provide more simplicity to code. 89 00:03:57.640 --> 00:03:59.720 However, just as you've seen there, 90 00:03:59.720 --> 00:04:01.900 they can also at times make it more complicated. 91 00:04:01.900 --> 00:04:04.870 So you do have to be careful with lambda functions, 92 00:04:04.870 --> 00:04:06.020 when you're using them. 93 00:04:07.460 --> 00:04:08.610 Let's look at an example. 94 00:04:08.610 --> 00:04:12.070 Here we've got some code where we define a function, 95 00:04:12.070 --> 00:04:12.960 called average, 96 00:04:12.960 --> 00:04:15.290 that takes in any arbitrary sequence, 97 00:04:15.290 --> 00:04:17.310 and returns the sum of its elements, 98 00:04:17.310 --> 00:04:20.160 divided by the length of the sequence. 99 00:04:20.160 --> 00:04:24.120 So, for example, if we iterate over of list of students, 100 00:04:24.120 --> 00:04:26.410 we can then call the average function, 101 00:04:26.410 --> 00:04:28.690 with each student's grades. 102 00:04:28.690 --> 00:04:29.600 And what that's going to do, 103 00:04:29.600 --> 00:04:32.920 is it's going to take these grades, and then these, 104 00:04:32.920 --> 00:04:34.570 and these, and then these, 105 00:04:34.570 --> 00:04:36.820 and it's going to calculate the average of each 106 00:04:36.820 --> 00:04:38.520 and it's going to print it out. 107 00:04:38.520 --> 00:04:41.110 Pause the video for a moment if you need a bit more time, 108 00:04:41.110 --> 00:04:44.010 to look at this code and thoroughly understand it. 109 00:04:44.010 --> 00:04:47.500 Just as we have defined our average function here, 110 00:04:47.500 --> 00:04:49.540 we could make this into a lambda function, 111 00:04:49.540 --> 00:04:51.630 if we think that's going to make things simpler. 112 00:04:51.630 --> 00:04:53.210 I think that certainly is not going to make 113 00:04:53.210 --> 00:04:54.530 them more complicated. 114 00:04:54.530 --> 00:04:57.013 So, maybe you could do something like this. 115 00:05:02.670 --> 00:05:05.130 This behaves I exactly the same way because 116 00:05:05.130 --> 00:05:08.990 this is a function that still takes in parameter 117 00:05:08.990 --> 00:05:12.670 and then adds it all together, and divides it by its length. 118 00:05:12.670 --> 00:05:15.910 However, it's fairly uncommon in python, 119 00:05:15.910 --> 00:05:18.710 to use lambda functions in this way. 120 00:05:18.710 --> 00:05:20.610 That's because when you have a function 121 00:05:20.610 --> 00:05:21.770 that's sitting on its own, 122 00:05:21.770 --> 00:05:24.300 and you're assigning it to a variable, 123 00:05:24.300 --> 00:05:26.500 you might as well just define a normal function, 124 00:05:26.500 --> 00:05:28.530 it makes it a little bit more readable. 125 00:05:28.530 --> 00:05:30.330 So, while you can do this, 126 00:05:30.330 --> 00:05:32.500 and this is one of those few cases where you might 127 00:05:32.500 --> 00:05:34.150 use a lambda function, 128 00:05:34.150 --> 00:05:37.390 there are better scenarios for using lambda functions. 129 00:05:37.390 --> 00:05:38.920 So we're going to look at some of those 130 00:05:38.920 --> 00:05:39.890 in the next video. 131 00:05:39.890 --> 00:05:41.930 For now though, I just wanted to tell you about 132 00:05:41.930 --> 00:05:44.250 the lambda function structure and syntax, 133 00:05:44.250 --> 00:05:47.610 which is the lambda keyword followed by any parameters, 134 00:05:47.610 --> 00:05:51.570 then the colon, then the return value of the function. 135 00:05:51.570 --> 00:05:53.930 And remember, lambda functions are used for getting 136 00:05:53.930 --> 00:05:55.930 inputs and returning outputs. 137 00:05:55.930 --> 00:05:58.500 And they're not used for performing actions, 138 00:05:58.500 --> 00:06:01.620 such as what the print function does. 139 00:06:01.620 --> 00:06:03.150 Thanks for joining me in this video, 140 00:06:03.150 --> 00:06:04.800 and I'll see you in the next one.