Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Python lambda expressions
0:00 Let's see this idea of lambda expressions
0:02 and these little computational, tiny methods
0:05 that we can pass around look like in Python.
0:07 So we'll start again with our main structure.
0:09 And I'm just going to paste in some numbers
0:11 because these are the same numbers
0:12 we were working with in C#.
0:14 And we don't have to write the print collection
0:16 we can just print these directly
0:18 and Python will print them.
0:19 So let's just see that everything's hanging together.
0:21 Beautiful. Unsorted. Unsorted, but there they are.
0:25 So, what if we want to sort these?
0:28 How would we do this in Python?
0:29 Well, we saw how to do it in C#.
0:30 We said numbers, nums.Sort
0:33 Oh that's right
0:34 the difference is the capital S versus the lowercase S.
0:37 Instead of passing just an argument
0:39 we have to do a keyword argument here.
0:41 So, the key
0:44 the element that we're actually sorting on
0:46 for any element in the list
0:48 it's a little bit different.
0:49 You don't compare two, you just give one back.
0:51 So here is where I would have written the lambda expression.
0:55 In C# I always say, n goes to, What was it?
1:00 I would say something like Math.Abs(n)
1:03 Now, Python, we don't have this syntax.
1:06 We have a better one, not better.
1:07 A different one. We see the lambda n
1:11 Well, it sort of triggers here's a function
1:13 and then from there to here actually it goes there.
1:17 Sorry. What goes before the colon is the argument.
1:22 So you could have n, n
1:24 but we don't need it in this case.
1:25 We don't have a goes to, we just say this.
1:29 So here's how we achieve exactly the same thing
1:31 as we had in C# but in Python.
1:34 Not bad, right? Let's see that that's true.
1:37 Look at that. Sorting ascending.
1:39 There's our -34 right where we would expect it.
1:42 Perfect. So, this cool.
1:45 I really like this. This is exactly the same idea
1:48 as we have for lambda expressions in C#.
1:50 Big difference though:
1:51 in C# you have two types of lambdas.
1:54 You have single line ones
1:55 like the ones we've been using
1:56 in the example I just showed you
1:58 and you have multi line ones
1:59 where you can use curly braces and actually do work
2:01 and then return a value
2:02 like more explicit lambdas.
2:04 Python only has the one line.
2:06 So, in comes an argument.
2:08 On one line you have to generate the return value.
2:10 That's it. It's a little bit simpler
2:12 but that's the big important kind of lambda anyway.
2:15 The other thing was
2:16 we did the Select statement with LINQ objects
2:19 using select on an i numeral.
2:21 That is not something that you can do in Python.
2:25 We don't have LINQ. That's actually unfortunate.
2:28 I really love LINQ. I wish we had LINQ objects.
2:30 But we don't. Nonetheless, we do have some interesting
2:34 similar types of expressions. So here
2:37 I could come over here and say doubled.
2:39 So we're going to create a new collection.
2:42 This doubled is going to be...
2:43 remember we had nums.Select(n => 2 * n)
2:51 Let's go over here
2:52 and I'll leave that for a sec.
2:53 What we do is a little bit different here.
2:55 We say we're going to generate a list
2:57 so we say 
2:59 and we first say the element that we're going to select.
3:02 So we would say 2 * n, right? That's this part.
3:08 for n in nums. And if you wanted to test
3:12 you could say if n % 2 == 0.
3:16 Like you could do only for evens
3:17 or something like that.
3:20 I'll put it like this for you. There we go.
3:24 In case you want to see what that looks like.
3:25 And then let's print out doubled.
3:30 There you go. That's the Python version
3:33 that we actually previously had done with LINQ objects.
3:35 This one actually generates a list
3:37 like if we ask, if we say print type of doubled
3:43 you'll see it's a list. If you want a generator
3:47 a lazily evaluated one
3:48 like you had with this one from LINQ objects
3:53 you make the slightest, tiniest change
3:55 you convert this from what's called a list comprehension
3:58 to a generator expression.
3:59 Remember the yield from keyword?
4:01 Well, yield in Python, yield from in C#
4:04 well, if it's square brackets it's a list.
4:06 But if you just change this to parentheses
4:10 then you print type of doubled
4:15 Now we have a generator
4:17 and printing it out like that doesn't tell you anything.
4:20 So you got to loop over it.
4:22 A quick way to just make this so it prints
4:24 is to throw it back into some list
4:26 or something like that.
4:27 We could loop over it and print it explicitly
4:29 but you can see the same numbers do come out.
4:32 You even have this lazy evaluation version like that.
4:35 If you want to put this in here
4:36 you got to change that.
4:38 I'll go ahead and change that to parenthesis
4:39 to get the same effect, okay.
4:41 All right, so these are lambda expressions in Python.
4:44 They always go like this. Lambda arguments
4:47 if they are None you just go like this.
4:49 Here, like that
4:50 and then it goes to a single line expression
4:53 that is the return value.
4:56 You're going to find that you can use these a lot in Python.