Python 9.4 : Callbacks and lambdas

We continue and here we will see the callback functions and the Lambda functions We will start with the callback functions, I will create a variable a equal to 4 here at the top I will create a def function function parenthesis colon Inside I'm going to do a print Hello And I will return 2 I want to call my function I run my code you see we have hello now I'm going to do a print two ribs a equals a b equals If I put I will make b equal to the function I will run my code you see we have Bonjour and we have equal to 4 b equal to two of return And if I remove the parentheses like this And I rerun my code You will see that in b we'll be told that it's a function I run you will see We have the value a equals 4 b we say it is a function so we just have the function with the identity of the function Now if here At the level of B I put the brackets I run my code you can see that it works normally as it did at the beginning so so this is what we call callback functions I'm going to comment on this and we're going to look at callback functions We'll start by creating a multiplication function Multiplication table as we did above table which takes as parameters n two points Inside I will make a loop for i in range parenthesis 1 , 10 that 1 is inclusive and 10 is exclusive so it will display a multiplication table from 1 to 9 And then I'm going to do a print Parenthesis star for the multiplication n And I'm going to do a comma two ribs equals times n And I will call the Function 4 I run my code you can see that our table works normally Let's imagine if we now make an addition table I will copy this code I paste And here I will put addition This will be a plus I will call the function I put a print to separate the two and I call the function table 4 I run my code We have the multiplication table and learn the addition table so our code works when we look at our two functions There is not too much difference between the two except for the signs the other is more and the other times Now what if we made a generic function instead? so I'll I'll copy this And here I'll put table display I'll leave the n And inside I will put the sign str I will put the callback operator and in the print we will have here The str sign str sign here the n and here we will have instead the operator callback operator which takes as parameters the i n we will now create a function here I will put def multiplication dash callback parenthesis colon which returns a times b I forgot the parameters and it takes a parameter a comma b well, first we will test the multiplication And then we will add the addition so I will comment I want to call the function table display and it will take as parameter 4 the value the sign I call the multi function callback I'll remove the parenthesis because it doesn't take anything in parameter I run my code We have well fed multiplication table which is displayed if I do the same for the addition I'll just copy this I'll paste it down here But if I'm going to call add here I copy this I paste at the bottom add and I run my code again here we still have the multiplication table so at this level I'm going to do a plus I rerun you see that our code works normally We have our 2 tables Addition and multiplication which means that these two functions can be deleted like this, I delete the calls, and now with the function table displays we can also make another function power here I'm going to put def that I call power callback inside I put a comma b colon this return a pow pow parenthesis a comma b That's how we do powers in python you can go and type on Google powers functions in python and you will have more details like this I restart you see we have our power function, we have our addition function and our multiplication function everything works normally now when we look at our code we see that we have several times returns in the different functions there is the return Now can't we simplify our code more. That's the question we ask ourselves. And this is where the lambda functions intervene we are going to delete this I'll start with the function multiplication we will put lambda voila and I'm going to put a comma b colon to multiply by b I comment this I run my code you see that we have the multiplication table. if I do for the addition I will copy this, I replace here instead I put more I run my code we have multiplication and addition correct I do the same for the power and here instead I use parenthesis a comma b like this I run my code See our power function our addition function and our multiplication function work correctly. so these functions are there to help us and make help us and make the code more in sync. And generally they are used on sites and applications to allow applications to allow us to execute the code while we are downloading files. files. Well, that's all for this video and we'll see you next time when it comes to variable parameter numbers.