Lambda4:37 with Kenneth Love
Anonymous functions are great for single-use functions. And they have a really fun name, lambdas!
def, is the keyword that marks a new function. Lambda functions don't have to have a name, though.
Lambdas can't contain new lines (outside of containers) or assignments.
Anonymous functions are functions that don't have a name, hence being anonymous. 0:00 I say that, but it's not a 100% true. 0:05 You can give anonymous functions a name in Python, but you don't have to and 0:07 usually won't. 0:11 We call anonymous functions lambda's, borrowed from mathematics lambda calculus. 0:12 Remember that whole, programming is done with expressions bit? 0:17 We've come full circle. 0:20 Let's jump over to Workspaces and see how to use lambdas and 0:22 how they can be applied to our existing functionality. 0:25 So lambdas are anonymous functions in Python. 0:28 They let us just write a function somewhere that we need one, and 0:32 we don't wanna reference that function again. 0:35 We can give lambdas names, so they're not anonymous, but that's a little weird. 0:37 Most people don't do that. 0:41 And lambdas are really only meant to be one line long, and 0:42 lambdas can't contain assignments, so we can't do a equals five. 0:45 You might be thinking, 0:50 well that's an awful lot of rules, why do we even wanna have lambdas then? 0:50 It's because they're really useful for little one off problems. 0:55 They're very handy for, I don't wanna write a function, 0:58 I just need to throw this thing in there, so let me do that. 1:00 For instance, all these places that we've been writing functions up here, 1:05 with the exception of the maps. 1:07 So, this map here, the title case, and this map here, the sales price. 1:09 We have to do assignment in both of these. 1:15 So we can't do these as lambdas. 1:18 But we can do our functions and our reduces and stuff as lambdas. 1:20 So let's see about that. 1:23 Let's look at this one. 1:24 We just did this total one, right? 1:25 Total equals reduce add_book_prices, so let's repeat that. 1:26 So let's do total, and it's gonna be reduced, and 1:30 now I haven't written a function, but my function up there just did a + b, right? 1:34 So, we're going to do lambda, and 1:41 we're going to take two numbers, x and y, just like in algebra class. 1:42 And we're gonna add x and y. 1:47 And lambda's automatically return the last value that they calculate. 1:48 They have an implicit return, so 1:54 you can pretend that there's the return keyword right there. 1:55 But you don't wanna put that in. 1:59 Okay, so this is our lambda. 2:01 So we have lambda. 2:04 These are the arguments that the lambda takes. 2:05 In this case our lambda takes two arguments, x and y. 2:07 A colon cuz we're starting a block, but we're not indenting the block. 2:11 Lambdas are a little bit strange, huh? 2:14 And then this is the body of our function, just x plus y. 2:16 And that gets returned automatically. 2:19 So now it's a reduce so we have our function and 2:21 then we have our list of values right? 2:24 So let's say b.price for b in BOOKS So 2:27 when we run this we should get 225, so we did. 2:34 So, we got the same amount of work but 2:40 we didn't have to write a function just to add two things together. 2:41 So, that's pretty cool. 2:44 Let's do our long_books again. 2:45 So, it's a filter and lambda is x. 2:47 Now lambda can be anything, we're gonna say book. 2:51 Lambda book. 2:53 Cuz we're gonna get a book, right? 2:54 So book.number_of_pages. 2:55 [INAUDIBLE] equal to 600 and we're gonna do this out of BOOKS. 2:59 All right, so now since I want to be able to print, 3:05 let's do len(list(long_books)). 3:09 So how many long books do we have? 3:13 We have 12 long books. 3:16 If I remember correctly, that was the number before. 3:17 So lambdas are really handy for being able to stick in places that we 3:19 don't wanna have to write a function that's only gonna get used one time. 3:24 We could do, good_deals = filter (lambda book: 3:30 book.price <= let's say 5.99, all right, 3:35 we'll do 6, so if the book's than $6 this time. 3:41 Out of books and we're gonna print len(listgood_deals)) and 3:47 let's see if there are any good cheap books. 3:52 There's 6, cool. 3:55 So 6 books that are nice and cheap. 3:57 So again lambdas are just something that you use instead of having to 4:00 write a function for something that you're only gonna do once. 4:04 That's where lambdas come in really really handy. 4:07 You might have heard the expression, when all you have is a hammer, 4:11 everything looks like a nail. 4:13 Well, lambdas are often a big hammer in the Python world. 4:15 You learn about them, and then you want to use them in all sorts of places and 4:18 situations. 4:21 Most of the time, a lambda is the wrong choice unless you're sure that you're only 4:23 going to need a particular bit of functionality once and 4:27 only in one location. 4:29 Writing our full functions might be a bit more trouble and take up more room, but 4:30 it will usually serve you better in the long run. 4:34
You need to sign up for Treehouse in order to download course files.Sign up