1 00:00:00,000 --> 00:00:04,300 Lambda expressions are a way to create a one time use function. 2 00:00:04,300 --> 00:00:08,530 So currently we have a function like 'def func_name', and it 3 00:00:08,530 --> 00:00:10,500 returns a thing. 4 00:00:10,520 --> 00:00:14,960 And then we can run 'func_name', and we can run this over and 5 00:00:14,990 --> 00:00:17,940 over and over and over and over and over and over again. 6 00:00:17,950 --> 00:00:22,950 And that's the benefit of a named function like this, is 7 00:00:22,960 --> 00:00:24,570 we can use it over and over and over again. 8 00:00:24,580 --> 00:00:25,950 That's actually why we name it. 9 00:00:26,380 --> 00:00:31,390 Now, a Lambda is more of a onetime use function where doesn't 10 00:00:31,400 --> 00:00:32,470 really need a name. 11 00:00:32,650 --> 00:00:35,560 And in other programming languages like JavaScript, this 12 00:00:35,560 --> 00:00:37,700 is actually called an anonymous function. 13 00:00:37,780 --> 00:00:39,490 So let's just jump right into this. 14 00:00:39,500 --> 00:00:41,860 Let's say we want to get the square root of something. 15 00:00:41,870 --> 00:00:46,310 We could create a function called 'square', take a number, 16 00:00:47,600 --> 00:00:50,900 and 'return num ** 2'. 17 00:00:50,920 --> 00:00:53,080 That is a way that we could do this. 18 00:00:53,920 --> 00:00:57,970 Or we could use a Lambda expression. 19 00:00:57,970 --> 00:01:01,600 And a Lambda expression is basically, you know you've got your 20 00:01:01,600 --> 00:01:05,300 variable name is equal to, and then the keyword 'lambda', and then 21 00:01:05,300 --> 00:01:06,400 your parameters in here, 22 00:01:06,400 --> 00:01:10,000 so we just have one parameter here, colon, and then it doesn't 23 00:01:10,000 --> 00:01:11,500 even take the 'return' keyword. 24 00:01:11,500 --> 00:01:15,000 It just assumes that whatever is after this, is what's going to be returned. 25 00:01:15,000 --> 00:01:18,300 So lambdas are really good for simple, simple functions. 26 00:01:18,300 --> 00:01:21,600 So we could 'return num ** 2' 27 00:01:21,680 --> 00:01:24,950 Now, if we just use 'square', we're going to see that this is a 28 00:01:24,950 --> 00:01:28,200 function, and it is actually a Lambda, and it takes one 29 00:01:28,200 --> 00:01:29,200 parameter called 'num'. 30 00:01:29,200 --> 00:01:32,100 That's not useful when we actually want to get the square 31 00:01:32,100 --> 00:01:34,300 root of something. So we could 'square()', 32 00:01:34,300 --> 00:01:35,900 and let's put the number 5 in here. 33 00:01:36,900 --> 00:01:41,200 25. We could 'square', let's do a big number, 909. 34 00:01:41,900 --> 00:01:42,900 It's a pretty big number. 35 00:01:43,060 --> 00:01:46,840 And if we wanted to, we could make another Lambda that takes 36 00:01:46,850 --> 00:01:47,800 more than one parameter. 37 00:01:47,810 --> 00:01:51,100 We could make a Lambda expression or an anonymous function 38 00:01:51,320 --> 00:01:52,580 to just add numbers 39 00:01:52,580 --> 00:01:59,300 if we wanted to. 'add = lambda num1, num2:', 40 00:01:59,300 --> 00:02:03,400 and it's just going to return 'num1 + num2', that's it. 41 00:02:03,400 --> 00:02:06,900 So we can say 'add(1, 4)', and this will return 5 for us, 42 00:02:06,900 --> 00:02:08,100 just like that. 43 00:02:08,100 --> 00:02:10,800 Now, that's overly simplistic. 44 00:02:10,800 --> 00:02:13,300 This one is also overly simplistic because we can just 45 00:02:13,300 --> 00:02:15,100 do that very easily. 46 00:02:15,100 --> 00:02:18,900 We could literally just say any 'varname = 1 + 4'. 47 00:02:19,160 --> 00:02:22,530 But if you have something that's a little more complex, 48 00:02:22,530 --> 00:02:26,800 or really only ever needs to be run once, you could absolutely 49 00:02:26,840 --> 00:02:28,270 do that as a Lambda function. 50 00:02:28,280 --> 00:02:31,370 But if you have something a little more complex, like you 51 00:02:31,380 --> 00:02:34,100 have a bunch of 'if' statements, and 'else' statements, and it's 52 00:02:34,110 --> 00:02:36,560 running other functions and stuff, you probably don't want 53 00:02:36,570 --> 00:02:37,670 to use a Lambda for that. 54 00:02:37,670 --> 00:02:42,500 Your Lambda expressions should be nice and simple. 55 00:02:42,500 --> 00:02:45,600 Now you're not going to be using lambdas probably every single 56 00:02:45,600 --> 00:02:48,200 day. I don't see most people using lambdas all the time, 57 00:02:48,200 --> 00:02:52,100 but they are vitally important in Python because sometimes 58 00:02:52,100 --> 00:02:55,300 you don't want that function to be available all over the 59 00:02:55,300 --> 00:02:59,300 place. Sometimes you just want it to be a one line, do a 60 00:02:59,330 --> 00:03:03,440 basic little math thing, or some sort of simple logic, and 61 00:03:03,440 --> 00:03:05,800 just assign it, assign it to a variable. 62 00:03:05,800 --> 00:03:07,600 And then once it's done, you can forget about it because 63 00:03:07,600 --> 00:03:08,900 it's a variable. Who cares? 64 00:03:08,900 --> 00:03:12,900 But once again, if your Lambda expression starts to get complex, 65 00:03:12,900 --> 00:03:15,800 that is actually a really good time to split it out. 66 00:03:15,800 --> 00:03:18,100 And by complex, I mean, if you're looking at a Lambda and you're like, 67 00:03:18,100 --> 00:03:20,900 "I don't understand all this other jargon that's 68 00:03:20,900 --> 00:03:25,200 going on over here", that's a good time to split it out into a proper function. 69 00:03:25,200 --> 00:03:28,500 But if you need a simple, one time function, Hey, lambdas 70 00:03:28,500 --> 00:03:30,700 are the best way to do that.