There is a function in Python called Lambda function which is very useful. It is used write anonymous functions. Anonymous function is a function which does not have a name but it is an expression.
What am I talking about? When and how to use Lambda function? Let's go to the computer screen.
And let's get started. [ MUSIC 🎶] Lambda functions are single expressions. For example, we have an expression such as a + b.
In which value of a is 1 and value of b is 2. A + B is an expression whose value is 3. Lambda function are very similar to expressions.
I’ll show you. So, I'll start by creating a function called def(sum). I can also name the function as avg.
But I’ll name it as double because I've already used it in an example. The example was x*2. If I write print(5) then you’ll say don't teach this.
I know that you all know this and it will print 10. Which is correct. Sorry, I should’ve written double(5).
You'll say that the value will go into the function and it will print 10. Now, what to do? Listen to me carefully.
Instead of writing double like this I can also write it like this. I'll write "double = lambda x: x*2 ". Which takes x as input and returns x * 2.
Now, you’ll think that are both of them the same? Yes, they are the same as you can see. This is Lambda function.
That's it. If you have listened carefully what I’ve said till now. Then you’ll understand what Lambda function is.
We are creating a function variable and using it as a function. Either I can write these two lines. After doing this much indentation.
Or I can also write "double = lambda x: x*2 ". Now, you’ll think what is the difference? You could have written it in two lines.
Some people will argue that this is more readable. But according to me, if you have to make mini functions like this in your program. So, Lambda functions are the best approach.
Lambda functions are used to transform a value. For example, multiplying the variable by 2 or the square of the variable. If you want you can also get the cube of a number.
For example, I'll make a function called cube. You can easily make a function using the def method. And I am not judging you.
It is a very big achievement that you are following the 100 days of code till now. It will take the value of x and return the cube. There is no need to write return or def.
Nor I have to think about the indentation. If I print the cube of 5 then the output should be 125. The output is 10 and 125.
You can also make this function using the def method. By writing return x*x*x. Then you’ll have to do the indentation.
So, this is Lambda function. Now, some of you will argue that how can we write Complex functions in Lambda function? Do you have to write each and every function in Lambda function?
It is not like that. You can write function just as you have written it till now. This is a new method.
You'll see this in most of the places then don't get confused. This is Lambda function. You thought that it will be difficult and complex but it is not.
But it is this easy as I’ve shown you. Can it take multiple values? For example, if I try to create a function called avg.
So can I write lambda x,y: and then it should return X + Y / 2? Is this possible? Yes, it is possible.
If I want an average of 3 and 5 then it will print 4. As you can see 4. 0 is printed.
No problem. Will it take an extra value if I also add variable Z? Now, I want X + Y + Z / 3.
I'll give the input as 3, 5 and 10. So, the total of 3 + 5 + 10 is 18 and the average should be 6. Will it give the output as 6?
Yes, it will. In this you can also give many values. You can also give 5 to 6 values.
You can also write multiple lines using brackets. But the thing is, do you want to do this when you have multiple lines of code? Are you willing to compress the logic?
The answer is no. So, when should you use Lambda function? You should use it when you want to write function in a single line.
In fact, we use Lambda function when we pass the function as an argument. Don't get confused. We can pass function as an argument.
Can we pass a function to another function? The answer is yes. For example, I'll make a function and name it as "apply" or "appl".
I’ll give this function another function. Then it will return 6 + that another function. fx(value).
I'll write value here. So, now if you want to print the appl function. I'll pass the cube function and the value 2 inside the appl function.
Now, cube function and two will go inside this function. Now, it will give 6 + cube of 2 as output. The output should be 14.
As you can see, 14 is printed in the output. You can pass a function inside another function. Now, I can make a function using this instead of using the def method.
I can pass another function. Using def I would have written multiple lines for this same function. But here I have done it in a single line.
Don't get confused. Try to adapt the thing that I told you. About passing a function inside another function.
Say it to yourself that it can be done and then try it yourself. Practice it by writing a program. Don't just watch the video and be done.
Watch, execute, take a break and then take notes. Please read this which I have written for your understanding. In Python, a Lambda function is a small anonymous function without a name.
It is defined using Lambda keyword. It is the syntax. Lambda functions are used in a situation where small functions are required.
For a short period of time. They are commonly used as arguments to higher order functions. I already told you that we use this when we need to fast higher order functions.
The next video will be map, filter and reduce and you can see it in that video. Then we'll talk about it. As you can see I have written an example here.
The above Lambda function has the same functionality as the double function. But Lambda function does not have any name. Here, we gave it the name as appl but this has no name until and unless we use “==”.
For example, if I passed the cube function directly inside this function. This function is for cube but it has no name. We didn't use the “cube” word while passing it.
I have created a function called cube over here and even if I didn't created it. And I would’ve written it directly then also it will work. There would have been no need of making a function called cube.
A function with no name. An anonymous function. Instead of a cube, I'll make a function called square.
I have not made a function called square till now. But here I have passed an expression which will work as the square function. As you can see I have not created a function with the name square.
But I am getting the same output which is 10. So, I am passing a function without even creating or naming that function. So, this is anonymous function.
A person with no name is called anonymous. Now, if I wear this mask then I'll become anonymous. You know my name but if anyone comes with this mask.
Then you not be able to recognise their name until you see their face. It is simple. As you can see here that Lambda functions can also include multiple statements.
I already told you this over here. It is very similar to regular functions and I have told you the differences. Go through this slowly and try to digest it.
In the next video we are going to discuss map, filter and reduce. If you haven't accessed the play-list yet please access it. Drp your reviews in the comment section of the first video.
That's it for this video. Thank you so much guys for watching this video. And I'll see you next time.