Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Concept: lambdas

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's take a moment and talk about a core concept called lambdas. In Python, functions and methods are first class citizens.
0:09 That means they have their own type, they can be passed as arguments, and they can be treated as like a class or other elements in the language,
0:18 and so that makes it very natural to write a method like the one we have here, find the significant numbers, to find_sig_num, so if it's on the slide,
0:25 you pass it set of numbers, and then some kind of method and you can use this method to test is this number in this set or is it out of the set.
0:34 So this is an example of using functions as a first class citizen and it turns out often when you write code like this the little bit of test,
0:43 the predicate you need to write and pass along is really specific to this one purpose where you are using it,
0:49 it's usually very simple and it often does not make sense to make its own dedicated method, you just want to kind of say here is the little test,
0:58 and it's lambdas that let us do this, so you can see on the second to last line we write lamda x: x % 2 ==1
1:05 so the test we are passing off to this find significant numbers, in this one example is find me all the odd numbers.
1:12 So you can see we have the Fibonacci sequence, the first bit of them, we are passing it off to this fine significant numbers
1:16 and we are only looking at the odd Fibonacci numbers, and that's exactly what comes back, you'll find lambdas to be a key part of the language,
1:23 and the more you dig into Python, the more you see that they are up here and they are helpful all over the place.


Talk Python's Mastodon Michael Kennedy's Mastodon