[Music] hello so we're still in the function chapter um and we've seen how we can use this like death star arcs of death double star arcs star args double star car works um so as i was saying it also works the other way around so let's say we are defining a function like i can do dive on my function uh and oh we can just yeah i get the function we defined before let's say uh a b and c uh and then we will uh return a plus b plus c i a plus b plus a plus b plus c so here's my function sum uh yeah i need a double dot here uh so this is how it works simple function i will see how this function is defined with fixed parameter a b and c and here i have my args and coworks so what i could do is i could create a list and my list would be equal to 1 4 and 5. so this is my list and you know you have your function here and you have a list and you have a function which do a b and c and you're like oh i would like to apply my function so i get a plus b plus c or you know a plus b minus c let's say uh so you have this function and you're like how do i put this argument there so you could just pack you could do like lists of zero list of one list of two but it is also quite i would say tiring to just write all of this so instead you could just do add my function and you will do star of your list so if you do that you do my function list so list will be passed as an argument and it will read first element of my list second element of many third element of my list a b and c um same we could also do it with this uh double axis so with the skywalks just like dictionary so i could create my dick uh so let's say you know for um for let's say we're having this like a convert money right so as you convert money here and um i'm just so i have convert money and i have my amount so let's say my amounts is a dictionary so i will get an amount so i will get an amount which is 57 67 i will get an origin origin currency origin currency our agent currency that will be equal to euro and i will get a destination currency that is equal to usd and i need to put a destination here destination so this is um origin currency i need to put a quotation mark so here's how it would be defined i have my stuff and then you know before we use this like combat 100 and stuff and i could use convert money and i will do star star amounts so if i do convert money i will do 67 and i will convert money from amount which is stored in dictionary um why is it practical because sometimes your data is written in a certain way so your data is a dictionary you detail the list and your function is taking single elements um because your function is your function it has been written by someone else etc and this also works for a built-in function if you have printing function that you normally take element y by one you could um like a b c d and you have a list you can just pass your list with the star uh this star meaning i'm going to take the first the second the third argument same for the dictionary it has to be equal to the stuff but yeah it's um it's a very practical way to write and to adapt your function to the data type you're having um so this is how it works you know with the function of a b c and d and let's say of my arguments one and two so that's what we've seen they are as a list and then i can you just apply my function to arguments so what i did here uh with this stuff my function of my list and then if we get a dictionary a and b so a and b written as string because they are not defined before then i will use this like bubble style x because it's more like a dictionary type so before python 3 we could have written something like apply f args so we would be like i want to apply f to this type of argument always a double star but in python 3 it disappears so i can't show you that now um so another things that i've speak is about functioning function so i already mentioned this a bit before um so let's say we have my function a b and c which return a plus b plus c so this will return the sum so some numbers so let's say you have some numbers yep some numbers so let's say i have some numbers and then i will do another function def product product of the sum let's say you have the square of the sum you know i will get square of the sum so square of the sum and square of the sum would get a b and c and c and then uh i got a b and c and i will return i would create the sum so i will be sum is equal to a plus b plus c and i will return i is some all power too so this will be the square uh of my stuff um no i here you know i mean it's only two lines so it's going to take us the same time it could be you know that this function is way longer and i already have a function to calculate the sum so what i could do here is like i do sum equal some numbers of my a b and c so this is also something that will work so if i do square square of the same 2 3 4 then i will get 81 because 2 plus 3 plus 4 is equal to 9 and 9 times 9 is equal to 81. so this is um as well something possible to do to have a function in another function i will call this function here i will save the variable and then i will do operation on this variable and return it um so it is quite particular i already um speak about this element before when i was thinking uh speaking on how we can you know if we have a function that is too big we can split it in smaller part and have like a function for the different part of it so it's a bit more nicer to read etc so you don't want to have function of everything you know um it's for like so small stuff you know it doesn't it's you don't want to over-engineer the thing but you also want to make your like your life easier so it's also something you need to take into account when you're thinking about like how what what should i do how it works etc um so we speak about the different type of function so we see built-in functions so for instance here i'm creating the sum of my numbers this is a user-defined function but i could also do uh some of my list i think i was having a list variable um which was defined uh i think i defined it before i have a list they're just there yeah i have a list well i'm just gonna print it so i have a list uh which is one four and five and i can do some so this one um is called a built-in function because it comes in python bluetooth it doesn't come from a module it doesn't come from anything else it's just like it there um so that's it for this built-in function you get user defined function so this one square of the sum some summer are user defined function meaning i use this like dev stuff to to to define them and the third type of function are anonymous function how does anonymous function work so um i mentioned a bit anonymous function in the past when we're seeing the variable as like lambda is a variable um so i'm going to speak more and know about the selem dat function the lambda function is a small anonymous function and it's also some things that make python quite strong and it's very when you do operation and data it's a function that you want to be able to use so the function is having any number of arguments but can only have one expression so we're going to see how we can use this lambda function and what it does so we've seen that when we're defining a function we're using this like def name of my function parentheses with more arguments uh and then double dots and then i have an indented block so lambda function is i would say bit smaller so here i will define a lambda function called f so i will do f equal lambda of x double dot x plus 3.
then i do this it will be oh i create a function which take x as an argument and return x plus three is doing this operation and then i write f of three so if you want it's a bit like um how when you define function in a mathematic way it would be oh i have an entry and i have an exit and i'm doing this operation so this is closer i will say to a mathematical form of defining functions and remember the guide building python in math so i think that also what what is behind this and then you call f of three so you really call it um as a function as well so you save uh this uh function uh in a variable so f is going to be uh you type for lambda function uh and then you could also imagine that you don't have one argument which you have two you know it would be uh f of x y f of a b so you do g for instance here you will do lambda a comma b equal r time b plus three so i can show you how this anonymous function will work in python so lambda functions so length functions functions so here i got my lambda function up um so what do i do i'm having my f and i would just do f equal so i using lambda so you see lambda is not becoming green right so we'll do x and you will just do x plus three so here i'm defining a lambda function i could check you know i created this variable f so i can check what is the type of f so f is defined as a function uh here f is not defined as the output you know if i do f of three then this is sex and the type of it um would be an integer so basically lambda is a function that you know could take any argument that is compatible with operation i could also do um i can do with a string but um it will work with any number so if we take a parameter from what i see here numbers um and we perform the operation x plus three on this entry f is a type function but the type of the result is it so just to know that f and f 3 is not the same f is defining a function and f of 3 is a result so it's already an input and it's the output of it um i can also do f of 5 etc etc that's how it will work now we've seen in the lecture we can do f i got lambda and i'll get a and b and then i will do a time b plus 4. so if i do this and then i do type of g so type of g is a function if i just call j it just say like oh it's a function uh that have lambda a and b you know and then you could be like um g of five and six and it would be 36 and you could also imagine being something else so you could also have function like let's say h uh which i'll taking although parameter relax so let's say h of l uh is going to be equal to length of l uh so h well i mean it's this is not plus three let's say press four so you know you could do like um have like h of uh hello and then this would be nine if you add some more characters then this will be twelve so it can really be um any uh any any type here of l you could also do l plus um hello u uh and then uh it will just concatenate the string hello and you uh so they say something as well that is possible with this like f and j function um so you have a different stuff um one of the last part of this lecture is about recursive function so we've seen how we can define function that are basically um calling themselves here inside but what we haven't done uh what we haven't looked into is like record save function so recursive function are um is a function that is calling herself so recursive function um i'd say on an abstraction understandable side can be quite tricky it can be quite tricky to understand how these render functions are working um what you want to keep in mind is um they are very practical and it's good that you're able to understand uh what a record state function is doing uh so we're going to go together um to a recursive function um and um we're gonna see how it works so um let's say we have um we're gonna the goal is to calculate the factorial so we're gonna derive given given a number n we want to derive the l factorial of this number so we say given a number and we want to derive the function of this number so i will do the factorial factorial of a number so n uh what is a factorial a factorial is like uh if i have five the factorial of five uh is like this operation right but it's not known in python so five uh is equal to five times 4 times 3 times 2 times 1. so so if i got n n factorial so the factorial operator will be equal to n times n minus 1 times n minus 2 time time time time time time until 3 and 0 2 until 1.
so this is how what the factorial function is looking uh how is this factorial function used so factorial function are used most of the time in probability or they are also used um yeah in statistics so it's mostly to have an understanding of combinatorics so it explains the number of possibilities you have of permutation you have within something um but yeah so to illustrate it if you have a number eight factorial of eight will be equal to eight times seven times six that five times four times three times two times one and factorial calculation is used in probability i will say most of the time um it will be used in this kind of case i mean where you might see it so i want to calculate a factorial of m so i think we know how to do this function uh we can do it with what we know so if i do it with what you know what do i do i will get okay result is equal to maybe one you know and then we do for e in range of n plus one and plus one and we do print and you know so if i do this if i just do this uh and then i do a factorial of five so um a print eye so if i do this you know i have result is neutralized to one then i do four e in range of n plus one i print e the first time is going to be equal to zero oh we're going to start that one um we're going to start at one then we get twos and we gain threes and we get fours and we get five we can also start at two but whatever uh is better if we have one at the entrance so let's say a factory at five um here so i print and what do i want i want my results so i want my result to be equal to result time my new number i right so if i do factorial 5 i will return result return result so what i do here i go to all my number from one to my number n which in my case in the example i'm demonstrating is five um so here i can print my result print my result and then when i do factorial phi i do see that the first time it is equal to one the second time one time two it is equal to two then i do two times three it is equal to six and uh then i do time four it is twenty-four um so this says uh how it works so i can do print result and i uh so i do see that i need to oh yeah um so this is how it works so my result will be 24 times 5 and 24 times 5 is 120 um so this is how it will uh work uh with a full loop so you know i give my factorial i'm going to a loop i'm going for all the number four four four four four from one then i have two time two i do time three i do term four i do time five etcetera so this is how a factor yield will look and now let's say i will do um factor factor of six sacrifice six which is b hundred twenty four times six because six six uh so if i get six six factorial of six is six times five times four times three times two times one i'm going to show it here so factor l of six that's a factorial of six factorial s is equal to sex times factorial of 5 you know because it's 6 times 5 because factorial of 6 is equal to 6 times 5 times 4 times 3 times two time one so this is factory losses i'm gonna put this small down so it doesn't so yeah so this is like um how factorial of uh six is looking and how the other one all looking uh so you recognize here is that when i drew factory lobsters it's like six times factor yellow five and that you will see that uh this is five times factor yellow four so this is going to be five times factorio of four so this is also how this suites work work i got my six and then and i can go fourth and fourth i will do like so this is time four time factorial of three and factor of three is also three times factorial of two so it's gonna go three times factorial of two and this is also two times factorial of one two times factorial one and factorial of one we know it is equal to one and we know and we know and we know that factorial that one factorial is equal to one uh when we use here's the exclamation mark it is a sign for factorial so that's what it means so if i do factorial of 6 it's equal to 120 times x so i get 720. this is it there is another way to write this function and it's called recursivity so it's when the function is calling itself so i see that factorial of 6 is 6 times factorial 5. so i'm like maybe instead of going through a loop i can use use this recursivity of life every value depends on the one before so i can use it in my loop so we could define a new function i'm going to show you where it works it called recessivity factorial and we call it right for recursively then saying it's going to take a number so we've seen that if n is equal to 1 so we do see that if n is equal to 1 then i will get f equal one so i create a variable that is f equal one uh else so if if i'm not equal to one so if my m is not equal to one what do i do i do so if f for instance is equal to 5 you know what do i want to do i want to create a new variable that is equal to factorial rec time n minus 1 time n so if i do this it means that if n equal equal 1 which is my case i start with 5.
so n is equal to 5. this is not true i don't go there else i create f is equal of factorial and minus 1 times n what is factorial i minus 1 n minus 1 is 4 so factorial n minus 1 i drew again my function i do again my function i go there four is not equal to one else i return i have f is equal factorial n minus one so three time n four and then i'm again going there i'm going there with three and my f is looking like this um and then we do return f return f so if i do this and if i do factory and rec of five i will get 120. how you're like you know this structure of calling your function is audio function so we're going to type n is going to be the same it's just for comprehension purposes we're going to put it here uh entire factory realm so like how do i do here i'm going to print i'm going to add some prints you know so i see how my function is executing and you can see here we are going to execute execute the function with n equal and then i do this so um here we see that here we're executing the function first the first time we go to the loop with n that is equal to five the second time i'm going to my loop i'm doing f is equal and time factorial with four fourth and fourth until we go to one and in that case if s equal to one then i do one so i see that you know i'm iterating and i'm applying as a function with n decreasing know you like write morgan but um what is the value of f so you'll be like okay uh we're going to add another print and we will see how the value of f is looking good so here uh the value of f is and then we go there uh so when i executing my uh i have to refine it afterwards up up yeah uh so here what's happening is um we um as you can see what's interesting to see so here when i had this print it's a bit like a big break point of void is on when i do factorial error or n it means i'm checking if n equal equal one then i do f equal one i'm with five so i'm with five i go there nothing i'm with five i'm going there i print my stuff here is 5.
and then i do f is equal n so 5 in my case so here i'm going for myself i'm doing f is equal to 5 time factorial of 4.