# #100DaysOfWeb in Python Transcripts Chapter: Appendix: Python language concepts Lecture: Concept: lambdas

0:01 In Python, functions are first class citizens, and what that means is they are represented by a class instances of them,
0:08 particular functions are objects they can be passed around just like other custom types you create just like built-in types, like strings and numbers.
0:17 So we are going to leverage that fact in a simple little bit of code I have here called find significant numbers.
0:22 Now, maybe we want to look for all even numbers, all odd numbers, all prime numbers, any of those sorts of things.
0:28 But this function is written to allow you to specify what it means for a number to be significant, so you can reuse this finding functionality
0:37 but what determines significance is variable, it could be specified by multiple functions being passed in and that's what we are calling predicate
0:46 because this ability to pass functions around and create and use them in different ways especially as parameters or parts of expressions,
0:54 Python has this concept of lambdas. So let's explore this by starting with some numbers, here we have the Fibonacci numbers
1:01 and maybe we want to find just the odd Fibonacci numbers. So we can start with the sequence and we can use this "find significant numbers" thing
1:08 along with the special test method we can write the checks for odd numbers. So, in Python we can write this like so,
1:15 and we can say the significant numbers we are looking for is... call the function, pass the number set we want to filter on
1:20 and then we can write this lambda expression instead of creating the whole new function. So instead of above having the def and a separate block
1:27 and all that kind of stuff, we can just inline a little bit of code, so we indicate this by saying lambda and then we say the parameters,
1:35 there can be zero, one or many parameters, here we just have one called x, and we say colon to define the block that we want to run,
1:43 and we set the expression that we are going to return when this function is called,
1:47 we don't use the return keyword we just say when you call this function
1:50 here is the thing that it does in return, so we are doing a little test, True or False,
1:54 and we ask "if x % 2 == 1" that's all the odd numbers, not the even ones, so when we run this code it loops over all the Fibonacci numbers
2:02 runs a test for oddness and it pulls out as you can see below just the odd ones, for example 8 is not in there.

Talk Python's Mastodon Michael Kennedy's Mastodon