Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to discuss about lambda functions in Python.
Now what exactly is lambda function?
Lambda functions are small anonymous function defined using the lambda keyword.
Now whenever we talk about anonymous function it can be basically said a function without a name.
Right.
So they can have any number of arguments, uh, but only one expression.
Okay.
And they are commonly used for short operation or as arguments to higher order function.
Now here let me discuss about 2 to 3 keywords.
First of all, anonymous function.
Anonymous function is basically a function without a name.
And in order to create lambda function, we specifically require lambda keyword, and they can have
any number of arguments, but only one expression.
One expression basically means one logic, right?
So here is one of the syntax I've given it over here like lambda arguments colon expression.
Now let me just show you with some of the good practical examples so that you will be able to understand
how does lambda function basically work.
Okay.
Now let me do one thing okay.
Let me just go ahead and create a function which is called as definition a comma b.
Okay.
Now what I'm actually going to do over here is that I'll return a plus b.
Now here obviously I've created a function which is taking two numbers.
And uh, here clearly you can just write two comma three.
And here you'll be able to get an answer.
Let's say the addition of two numbers two comma three is fine.
Now over here this is just a basic function.
Right.
And inside this you just have one expression, one logic that you have written or one operation that
you have written over here.
Right.
And for this you have created entirely one entire function.
You have written the definition keyword a comma b, all these things you have specifically written.
Now, what it says is that if you have just one expression and let's say you have multiple parameters
over here, then instead of just creating a wholesome function over here, you can create a anonymous
function, which is also called as lambda function, a function without a name.
Now, if I really want to convert this function into a lambda function, see the syntax okay.
So first of all I will go ahead and write lambda.
And then the second parameter that over here it is asking is argument.
So what are arguments I am giving for this particular function.
It is very simple.
It is a comma b okay.
Now with respect to a comma b I need to go ahead and write my expression.
Expression is what task or what operation I'm actually doing.
Okay.
So once I execute this so you can see that this becomes an anonymous function or lambda function also.
So here I will just go ahead and create a variable.
And this addition variable will be my lambda function.
Okay.
If you don't believe me let's go ahead and see this type of addition okay.
And we'll try to print it.
So here you can see it is a function okay.
Lambda function.
It is not saying.
But here you can see that it is a kind of lambda function itself a function without any specific name.
Like we did not use any definition, we have just created a variable.
Now in order to call this okay.
All I have to do is that just call this variable, give the parameters that I want.
Let's say I want to give five comma six.
And let me just go ahead and execute it.
So here you can see automatically with the help of this lambda expression entirely.
The operation is basically happening here.
These are my arguments and this is the expression that we have specifically given.
Right.
So I hope you are able to understand and how beautifully, just by understanding the syntax we have
just given the values over here and you are able to see the answer.
Okay, let me do one more thing.
Okay.
Let me just go ahead and create one function.
Let's say uh, I'll create a function which is called as even.
Okay.
And here I give a number okay.
Now with respect to this number I'll just go ahead and put a condition.
If num modulus two is double equal to zero.
So in short I'm just checking whether the number is even or odd.
Then I'm just going to return the value as true okay.
Perfect.
Now let me just go ahead and give the value.
So here I'm going to call a function right.
Even 24.
So here I'm actually going to get true okay.
Now similar if I want to convert this into a lambda function let me just go ahead and create my variable
even one I'll be using lambda.
I'll give my parameters what is my parameters arguments that I need to give only num.
Then I'll write my colon and all I have to basically do is write my expression and the expression is
over here.
Simple num modulus two is double equal to zero.
Right now this even one becomes my variable.
So now if I go ahead and just go ahead and write 12, now it is going to give me true because 12 is
basically an even number, right.
And here you can write any variable.
It is not necessary that you only need to write number okay.
You can write A, you can write B, you can write C, whatever things you specifically want to write
okay.
Based on that you can actually do that.
Now uh let's try with some more examples.
And here this time I'm going to take multiple parameters okay.
So let me just go ahead and create one function which is called as addition let's say.
And here I'm actually going to give my x comma y comma z.
So these are my parameters.
I'm just going to return x plus y plus z okay.
Now when I'm actually doing this if I am calling my addition function.
So here you'll be able to see that I'll give my values.
So let's say 12 comma 13 comma 14.
If I execute it the sum is basically 39.
Now let me see.
Let me show you how you can actually convert this, uh, into an lambda function.
I'll create an addition one variable.
And now I will be using a lambda function.
And with respect to this specific lambda function, what I am actually going to do, I'm going to give
my arguments which all is required, let's say x y, z.
Then I'll be giving the colon.
And I only have to write the expression.
And I have already shown you how to write it down x plus y plus z.
Right.
So this is it.
And if I go ahead and call my addition one.
So here you'll be able to see that if I give my values 12, 13 and 14 I should be able to get 39 right.
So through this way, I am not wasting much time to probably create a function.
Instead, just by using a lambda function or anonymous function, I am able to do all this kind of expression,
uh, operations.
Right again, to repeat, guys, uh, lambda function where only a single expression is there, we can
actually convert that entire function into a lambda function itself.
Uh, lambda function has more amazing features, but for that we really need to understand about map.
And uh, there are two function which is called as map and filter.
So in the upcoming videos we will be discussing about this map function and filter function.
And I will just give you one example right now just to make you understand what exactly map and filter
basically does.
Okay.
So yes, in the next video we'll be discussing more, but I just want to give you the expression of
power of lambda function with map.
Okay.
So let's say I want to probably go ahead and create a list.
Right.
And with respect to the list I have values like 123456 Okay.
Now let's say that I want to find the square of each and every number.
Right.
If I want to find the square of each and every number, or let's say I want to just go and see that,
hey, uh, uh, you know, uh, let's say I'll just go ahead and create a variable, let's say squared
underscore numbers.
Okay.
Now in order to find out square for every number.
Right.
What is the kind of function we will define.
Let's say I'm going to define definition square okay.
Square.
And here I am going to give my numbers.
Or let me just give single number.
Then what this is going to do.
Return number double star square.
Right.
So here we are just giving a square.
And if I give two number I'm going to get the square of that particular number.
This is fine.
And this is only for one number.
Right now, if I'm giving for a list of number then what I have to do, I'll give this number as a parameter.
Over here I'll write a for loop.
I iterate through each and every number and do this particular operation and return it right.
But is there a better way instead of iterating it?
Truly, you know, probably writing more couple of lines of code for this, right?
Is there a better way?
So we can specifically use this map function.
map function.
What it does is that it applies.
It applies a function to all items in a list.
Okay right.
Or any collection also okay, so what does this basically mean.
And I will show you along with the map function okay.
So now sorry along with the lambda function.
Now let me just go ahead and create this lambda function.
So first of all I will go ahead and write lambda.
I know what is my operation.
My operation is basically squaring right.
So I will create a argument.
So argument I will be requiring a number x and the expression will be x square okay.
So this basically becomes lambda.
Right now map basically requires the first parameter in map is basically a function.
The second parameter is iterables.
So this actually is my lambda function.
Or you can also call this particular function.
But right now I'm giving my lambda function over here.
And the second parameter that I'm actually going to give is nothing but my numbers.
Right.
So here is basically my map right now.
At the end of the day, if I execute this, this basically creates a map object at this particular memory
location.
And map usually uses a lazy loading technique that basically means uh, still, uh, it has not completely
initialized.
So to make it initialized, I will convert this into a list.
Now see the magic?
Okay, for every number in this particular list, automatically you are able to get the square right.
Isn't it beautiful?
So I will just try to show you like how you can use Map Lambda.
And there is also one more function which is called as filter.
Right.
And uh, I'll explain all these things in my upcoming video.
So yes, this was it for my side.
I will see you all in the next video.
Thank you.