Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Python lambda expressions

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


Talk Python's Mastodon Michael Kennedy's Mastodon