#42 Python Tutorial for Beginners | Anonymous Functions | Lambda

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my master is ready and let's contend with the cities on Python now at this point we have talked about functions right and how do we define functions is by using death keyword and then we mention the function name as well right what if I don't mention the function name did I do that if I don't mention a name it becomes anonymous right and that's where we can create a function in Python called as anonymous functions functions without name all you can also call them as lambda okay so let's see how what that mean is so what I will do is let's say if I want create a function normally what we do is we say def and if let's see if I want to find a square of a number so what we do is we say Square and if we passed some parameters let's say if I pass a and on this a I want to perform an operation which is a into a and then the way you call them is by saying if you say user it is equal to square you call that function right and you pass a number so let me pass number 5 I want to find a spoil of number 5 and in this case I'm now so on to print the value for result now of course this should work right we have done that before it works you get 25 but to get the code now so we have this function definition right so now we talk about functions we expect it will have at least three to four lines of code or maybe five lines of code right look at this it only has one line of code what is doing is it is we take that value right so we have only one expression which is returning a value that's eight we are wasting two lines with our DEF keyword with a return keyword with a little function right so what if you want to use a function only once and you don't want to define the name of the function right and if you want to only have one expression can we do that when we talked about function there is one thing about functions we can pass functions to a function okay so that means the way you pass values the way you pass objects you can also pass functions because functions are object in Python if you talk about this square now look at this put it perfectly works right but the only problem with which I have here is I will not be using this coil function multiple times I will be using that's quite functional new ones and dad to it it has only one expression so it's not defining a function we can define a function when you want to use it directly okay you can simply say a : this is your function arguments okay so you're to say eh that's your arguments the arguments which are passing and the operation on that argument which is a into a right so we got this function which is anonymous okay which accepts a value a and what you return is a into a because you want to find a square of it but the thing is you need to use one more key word here which is lambda because the anonymous function that I also got is lambda expressions or like or you can say lambda lambda functions or lambda expression you can say whatever you want and that if you want to use that lambda okay the thing is as we mentioned that functions are objects in Python so of course you need to assign it to someone so I will say F equal to lambda now this F represents your function okay and you can pass that function to any function you can affect will be seeing that later so we can use this function in other functions as well you see that now what do I do want to do is I want to find a square of five so look at this we are simply saying lambda a colon a two a and that assigned to F now since this is a function we have to use a bracket right so whatever you pass here in five goes to a we will perform some operations on that a and then you will get it back now yeah I know at this point it looks bit weird but hold on once we talk about you know some other functions available how can we use lambda functions in other functions it will be awesome okay but time being this is how it works okay so if I love this code you can see the clock the output is 25 let's say if you want to define a function which will add two numbers okay we can do that let's say if I pass five comma six okay so we are putting the function f which doesn't have a name okay and you are saying five comma six and then that value will go into two variables right service a a and B and I want to perform the operation right so you will say a plus B so point to remember you can pass any number of arguments but it should be only of one expression which is in this case a plus B or a into a something like that it should be one expression so that's your anonymous functions okay so we had defining a function with the help of lambda and it doesn't have a name okay so you will understand this more when you start with when you see our next videos and next video we'll talk about how can they use lambdas in other functions okay so that's it from this video I hope you're enjoying this a let me know in the comments section so that's it everyone bye bye you
Info
Channel: Telusko
Views: 322,727
Rating: 4.9222779 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: hYzwCsKGRrg
Channel Id: undefined
Length: 4min 38sec (278 seconds)
Published: Sun Aug 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.