 Okay, so I planned it on this yeah, and then hi everyone again welcome to the LPS lecture we actually we went through several concepts on Python last time around the important ones are first of all we kind of talked about the tuples the list as data structure and then how we can operate on them that was the main focus from last time I think you guys remember that today we will be continuing with that actually the on Python programming essentially so Python programming itself we covered a lot of things so far so as you know we talked about the control flow the the the data flow itself with starting with various data structures the string integer types the floating point types things like that then in the last lecture we covered the two other new types like the tuples and the list and we kind of we went through how to operate on them how do we index them things like that so I think right now we have a good understanding of how the Python programming actually works so that we can take the next step of going to some of the advanced concepts so yeah we also talked about the collections and basically like what collections mean which is another data structure that we talked about yeah when we talk when we discussed tickle essentially and collections as a data structure of the synopsis supports that we talked about so similar collections are available in Python we talk about them so today what we are going to go through is basically some of the advanced functions essentially we are going to talk about how to pass lists and keyword dictionaries to functions then we also will talk about something called the lambda function it is a clear it's a function generator so we will talk about that then we will talk about some of the functions like apply map filter reduce and we also will go through some more understanding of the list like our list comprehension so let us begin today's lecture so how do we pass lists as arguments so the list can be passed in cases where there may be a variable number of arguments so this is something that we saw in a tickle as well or like we specify this special parameter in the and as a function and then you can pass the arguments so here there is a list r.py here we are defining this function as some the functions name is some and then we say basically the arguments are we denote it like this basically and then we say like the result is 0 for arg in args result plus r and then the return results so here since the star args is a itself is a pointer we can actually then pass a list into this so now like if you say like print sum of 1 2 3 the answer is 6 so note the syntax of how do we pass the arguments so then now the keyword arguments essentially like I mean so the caller of a function can place the arguments in the correct order so that is the positional argument so that is this is something that we saw even in a tickle as the positional versus the non-positional stuff so some sometimes we can actually change the order by assigning keywords so how do we pass that so for example so here like I mean in this one this example is a function with arguments in the wrong order but the arguments are passed with the keywords in text so that it does not matter so how do we do that so in in tickle we saw some newer constructs to handle this kind of an issue we talked about that now here let us see basically like so print hello basically and then we have the first name equals and then the last name equals and then we can say like okay pass the hello print the first name last name and then those are the two names that are passed into the function now if you want to so standard one is basically like I mean you can just write it like I mean what is for if you just say hello Sam or David shrill I mean Cheryl then it will print exactly the same but if you want to actually interchange the argument you can just say that last name equal to Cheryl and first name David as a hard assignment with these function names then it will actually print appropriate so even though here the order is interchanged basically the last name comes first this is first and then this is second what it prints correctly which is the first name and so this is one way to pass the keyword arguments essentially so when we have like specific like when we change the order then this may be useful so what happens if you are missing keywords and the positional arguments so this is also like something that we saw like how do we pass when we talked about tickle what if some arguments are missing we want to pass it appropriately there we use make the use of the tickles understanding of what strict substitution strings the curly braces and the quotes and then we made use of it to pass these kind of arguments so here how do we do it like in more Python so we saw already like the star operator which is used as a variable length of argument so we can use the same concept to designate all arguments after this as the keyword arguments so and this is something that is new in Python 3 so here we have an example again the area we say basically x y and then we say like the star operator and then we say units equal to inches so now then we do print x times y and then we say like square and then we say basically like square this particular units so if you say basically like 2 3 and then centimeters it gives you this square centimeters so the star essentially like I mean captures this so if you omit this then the units is taken as inches if you put it then the units become whatever that star that you specify and now the other important thing is passing the dictionary to functions as you know like dictionaries are the associative arrays that we saw in Perl and then we also saw similar concept in tickle and in Python they are called dictionaries I hope you remember that and we are going to be talking about dictionaries given in more details later on but here in this one essentially like we have this particular function called print and then we specify it now we specify two stars and then the kw args so this is the keyword argument so basically for each key in keyword args. keys essentially and then we can print what is the key and what is the value so here we say basically like okay so here is an example of user info is dictionary name user id and home directory there are three fields here so if you do say like I mean print date star star user info I think this one is print underscore text actually so and then the output is essentially like I mean user id is fine ID tree name is david and then the home directory is particularly from the like slash home slash slash david so one thing that you want to notice we talked about this earlier also like when we talked about the dictionary database or dictionary data structure the dictionary entries are unordered unlike lists or even triple for that matter so that is the reason why we even though we specify like this basically like the user ID came second the name of the first when it printed out basically user ID can be first and the name is second so since it is unordered this is what you may expect and the other specific thing is basically the when we pass the dictionary as an argument we need to specify with the double star now how do we pass the dictionary arguments essentially to function so the dictionary can be passed as arguments with normal function wanting the positional arguments so how do we do that essentially like so here again define the area as XY units inches and then here now we have this data in a dictionary so how do we pass that information so here we say basically like area info is this is the dictionary that we create which is x equal to 2 y equal to 3 and then units equal same so the three different keys basically x y and units and then their values are 2 3 and same now we can actually even though like this is a normal function we can pass this dictionary into this normal function by just specifying the double star area info the dictionary name and then output is just six square centimeters which is essentially the same two times three and then what's the centimeters square centimeters as in it okay so now we come to the another major topic that we want to talk about today all the lambda functions so the lambda functions are essentially they are shorthand versions of that statement or different statement per function it's useful for inlining functions that means that basically you can define functions on the go and also in other situations where it is convenient to keep the code of function close to where it is needed so where it is getting used one thing to note is it can contain an expression in the function can only contain an expression in the function definition but not blocks of statements there is only one expression essentially so we cannot specify like if statements or loops etc inside the lambda function and then a lambda returns a function programmer can decide whether or not to assign the function so this is another peculiarity even though the lambda function the programmer can decide whether to assign it to a function function 20 so let us see how we can use this lambda function so here is one example this is a very simple example so in normal course we define a function called sum x y and then we say return x plus y and then if you use it you can say like sum 1 1 2 and then this will result in three assets shows here so if you want to use the lambda function we can directly create like sum 2 equal to lambda x y and then we need to specify this column same as here just x plus y so this is like just a shorthand representation of this this line just here and then when we say like sum 2 1 2 that is also the same so we can use these lambda functions wherever the function objects are required there syntactically as I mentioned like the single expression semantically they are just syntactic should go for the normal function. Another thing is basically the lambda functions can reference variable from the containing scope so again like we can limit to that so another thing is basically we can say just make incremental and then this function we can define it as the term lambda x x plus so if you define a function like this then if you say like you can say f equal to make incremental 42 f0 will become 42 and f1 is 43 etc so you can call it this kind of a lambda function so you can use this lambda not just as a function but I mean in between functions for quick rather than like I mean actually using a function inside a function you can use like a lambda inside a function and get better coding that way so another example is like you can pass this small function as an argument so how do we do that so let us see like I mean we define a dictionary actually a list dictionary basically which is called pairs and then we say like the passes one one two say we have this pair and then we say like the sort the pairs dot sort and we define key equals and here we can specify a lambda pair pair is actually pair one now if you execute like pairs you can actually now sort the this thing in this case actually it will give you like one one and two two but you can actually sort it with actually like river sort from this one so basically like you cannot do the pair will now become so if you do the pairs it is essentially so two two so this is one one application essentially then you can specify the lambda expression to return a function essentially so now we go into some more details essentially and other functions essentially so here we talk about apply so in a in very general context you may not know ahead of time how many arguments need to get passed to a function this is like a typical scenario essentially the variable arguments and also like I mean if the function itself is built dynamically then you won't know essentially so the apply function calls the given function with a list of arguments packed in a triple so that is the usefulness of applying so here we can say basically like a function that means in some x y to the next one now we can actually call this to execute this function using apply so that it is basically we call we pass this this entire function as a triple that is some 3 4 and then the result is 7 the apply can handle functions defined with f or with lambda so that those things are possible as well so now also like apply can make the third argument which is the which is a dictionary of keyword arguments essentially so here an example name arguments start arguments we know that this list and then dictionary we want to print this whole thing and again here we have defined these two variables and if you want to print this essentially we can just say apply name arguments and AWR and then it prints the whole thing so again you can see that basically you can pass the function name the its arguments all in one shop and then you can get it executed so here if it is a variable name like a variable length of the fun the arguments you can still get executed with this function essentially like I mean the key thing to note with apply is basically it is the format is essentially like you know it is a function and then args basically and here you can have those keywords this is the general format of apply and one thing to note is actually like this apply itself is deprecated since version 2.3 and an equivalent function is just use function same function with star args basically and star star keywords in like directly this will actually work better in this context so now let us look at some other function this is what is called the map function so the map calls a given function on every element of the sequence essentially so once you specify the sequence essentially like basically it is kind of iterate through that sequence so essentially like I mean in other words it is basically applying the function to every item of the iterable and return a list of results so you can have a single function which will take a single argument but you can apply that to a sequence and then get the result so you can think of this as like a vector the function if additional iterable arguments are passed the function must take the take that many arguments take that many arguments and is applied to the items from all iterables in parallel if one iterable is shorter than the another it is assumed to be extended with none items so we saw that none actually previously and so the remaining will be treated as none for that particular and if function is none the IDGT function is assumed so if there are multiple arguments map again returns the list containing a consisting of tuples containing the corresponding items from all the it is just a transpose at the point the iterable arguments may be a sequence or any iterable arguments and the result is always a list so that is another thing that you may want to look at so here is an example it is called double X return times 2 so if you want to apply to this sequence like 1 2 and 3 all we got to do is the map and then the function name followed by the iterable which is here in this case it is the sequence a and then you get like 2 4 6 as a list so this is the list this is the iterable that is the function so the maps a general thing is basically map you have a function and then set of each of these okay so what this means is like I mean if we have another set called b equal to say 1 2 3 4 and then if you say map double a comma b so now the answer is going to be 2 2 4 or 6 6 then 0 or none 8 so that is the list that you are going to get and then we can also like add the lambda inside this map function so here directly we write lambda x x times 2 and then a and then the same result so here we do not have to specify this x equal to x plus x times 2 this is a another function for double so omit this double and then directly with map this you get the same answer so some more on map so here this is something that we saw earlier we can also like add these two essentially like so here we specify two variables basically x and y x plus y is the thing so here the sequence is assumed to be x and this sequence is assumed to be y so you get like every pair added and that will be the result so 1 plus 4 5 2 plus 5 7 and then 3 plus 6 is 9 again here if you omit one of them say like I mean 1 comma 2 and then this is 4 5 6 then our answer will be like 4 I am sorry 5 7 and then 6 because third argument is assumed as more so you go and error out saying that they are two different size arguments it will just generate that this result so now the the next one is filter so these are all like built-in functions there are many many many built-in functions in python we will go through like some of them let us see like I mean we may be able to go through like some more later on so the next one is the filter the filter command essentially if it works on lists unlike map it returns only the selected list of items so we saw the filter commands in tickle to specify like I mean some way to restrict the data so similarly like we can also use filter in python and then the filter command essentially like I mean it is basically like so you can specify filter as filter function and then the interval so this is a typical specification or format for filter command basically what it says is to construct a list from those elements of the interval for which the function returns a true interval must be either a sequence a container which supports a iteration or just an iterator and the interval is a string or a typical the result also has the same type otherwise it always it returns a list the function if the function is none basically like here there is nothing none then the identity function is assumed and basically just copy it back so the same thing is just written so all elements actually like I mean the all elements that are all they are just removed and then you definitely the all the true in so that is something that you may want to know so the other thing about filter is also basically like a specify like function and interval it is equivalent to item for item in iterable if function item so this is the same as item or item in iterable if function so you can think of filter as a shortcut for this command basically item or item in iterable if function item so the function item return to either true or false so if this is true then that becomes the item from for and you iterate inside this iterable all all the items look at all the items so that it is kind of you can you can think of this basically has how it is actually written and you can also add additional condition basically that is you can say like if function is not none and the item for item in the iterable if item basically like so it also tests for whether item is true or not if function is not okay so that is the second condition that I talked about so here we have an example it is basically a filter lambda x x you know this one basically if the reminder essentially like from that region is equal to 0 and then we give this range that is minus 4 to 4 that is your iterable the range is a iterable and then the function is yeah so now we will see like I mean how this thing is this thing works essentially like so it iterates from minus the four to four and each one and then basically it adds by two and then compares the reminder so whether that is equal to zero so for four minus four the remainder is zero so this becomes true and that is filtered out next one is minus 3 and the minus 3 actually like now the remainder is not 0 it is actually minus 1 so that is omitted and then when you go to minus 2 again it is 0 so it prints 2 and then same like 0 and then 2 so it prints out all the even numbers within this range and if you look at this one there is one thing is basically it is not the last one is not listed here basically only up to 2 it is not the four is not there that is because if you recall the range function does not include the last value given in the range so it is like n minus 1 so you may want to pay some attention to this essentially when you are actually writing the program because if you want to include that four then you need to extend the range past the four okay so now we go to another function called the reduce reduce is just like map but it reduces the list to a single value and then this is using some operation so again it is a function and the iterable but here the function is essentially like I mean just go ahead and add the or basically like I mean it there is a it essentially like gives the last so so the so it reduces this list into like just a single value so each operation acts on the result of the last operation and then the next item in the in the list so here we say like lambda x y x plus y so basically just first one is like I mean it is again 0 so it is basically 0 and then it adds plus 1 plus 2 plus 3 plus 4 and then the final result is 10 so now let us see some the list comprehensions these are really not functions but they can replace functions basically maps filters etc so essentially we can specify the list as essentially like x power 2 or x in range 9 so it gives you all the square numbers from 0 to 8 as you know like I mean 9 is excluded from the range so that is what you will get and then we can also like to get idea of actually like a square number and then which is also divisible by 2 and or even square numbers that for that we just specify this which is like all the way to up to 9 so it prints 0 4 16 36 and 64 it means 1 9 25 and 49 so essentially like I mean so it is like functions that you are putting it inside a list but it is expanded and basically expanded as a function and then the with the real operation that goes on now the other one is x plus y for x in 1 2 3 and for y in 100 200 300 the result is basically adds up x plus y for each of the x so 101 201 301 then 102 202 and 302 and then finally 103 203 303 so this is the result so we we talked about the iterables so there is a concept of generators and the iterators sometimes it may be useful to return a single value at a time instead of the entire sequence so for that we use the generator essentially generator is the function which is written to return values one at a time the generator can compute the next value and save its state and then pick up again there it left off when called again so syntactically the generator look just like a function but it yields a value instead of returning one so and a return statement will actually terminate that sequence at all sequence all together so until we see a return statement basically keeps that single value around so here is the generator example so we define squares essentially and then for i in range x yield that is the the new one with the generator and then i power 2 and then we basically use this as for i in squares x and squares for print time so now it is going to print just one value at a time essentially that is 0149 and it keeps the last value at this point and then we can get the next element in the sequence from the next function so v is squares for and the first one first time v next when we use it it becomes 0 which is the first one and then that v next will become one and then so and so so now the persistence of mutable default arguments so the default arguments which are mutable versus between the function calls song essentially so these are like static variables so if they are like mutable default arguments they continue between the functions and this may not be the behavior that we want so if we do not want this behavior we need to copy the default at the startup function body to another variable or move the default value expression into a body function and the body of function so this is basically like something that you may want to pay attention again this is another quirk of firm python because of this we want to do this so now that finishes this this lecture so in this lecture we actually went to a couple of things one is mainly the lambda function then we also looked at some of the default functions that are available inside python the few ones are like the apply which is kind of deprecated right now you can use just the function call just like what is specified here as directly and then the map function then we also looked at the filter and then reduce we also kind of learned how to write functions in form of the list basically so if they are not less than we actually write functions and it is comprehended as a function and it is applied as a function so we can actually like write these kinds of commands into a list syntax and then call it as a function and then finally we actually learnt about the generator as a concept we still need to look at the iterators which we will do at a later stage but so and then we saw this example of how to use the generator and then finally one of the rules of python is essentially like any kind of default arguments they persist between the function calls if they are mutable so if you do not want this behavior then we need to copy the default at the start of the function body to another variable or we have to move the default value expression into a body of the function so inside or outside change to another variable so that is pretty much the lesson for today basically the lecture and again we will pick it up in the next class thank you