Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Concept: Lambda expressions
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Functions in Python are first-class objects. You can pass them around, and one way you can define them are through these lambda expressions.
0:09
So here we're sorting this in Nums, a list of numbers and we want to specify how we're comparing each individual element.
0:16
So we just create this lambda expression given an input, or argument n, what do you want to do? Here, we're actually going to sort
0:23
by the negative amplitude if it's an even number and the positive amplitude if it's an odd number. It sorts exactly as you would expect:
0:32
the first four, five numbers are even so they're from largest to smallest and then it switches to odd so those are all sorted to the end
0:40
and then from there it goes up, up, up, up, up even though there are some negative ones up near the top their amplitude is, of course
0:48
like, amplitude 233 is bigger than 89. How cool is that?