 Hi, my name is Swastia Matau and today I want to tell you a little bit about using arguments, keywords, argument lists and keyword dictionaries in Python. So basically I want to tell you all about how you can pass arguments to functions. Now I made a very, to demonstrate, I made a very simple function here, move, that takes four arguments, an x-coordinate, a y-coordinate, so the starting position, the direction in which we're going to move up, down, left, or right, and a distance. So the amount, the number of units by which we're going to move. And the logic of the function is very simple, we say just check if the direction is up, then we increase the y-coordinate by the distance, if the direction is down we decrease the y-coordinate, if the direction is left we decrease the x-coordinate, if the direction is right we increase the x-coordinate, and if the direction is anything else we raise an exception to indicate that the direction was invalid, so we're going to be safe, we check if our coordinates, or if the directions are sensible, and then we return a new x and a y-coordinate. Now so how do we use this function? Well first we define our starting point x and y, we just start at zero zero, we print it out just so that we see what we're doing, and then we say okay we're going to move our x and our y-coordinate, we're going to move up by one unit and then the return value we store again as x comma y and we print out the new position. So if I select all of this and I run it you will see in the debug window that we've moved from zero comma zero to zero comma one, right, so we've increased the y-coordinate by one, which is exactly what we do want to do if we move up. Now so what does this tell us about the arguments? Well the point here is that these arguments they are passed by order, right, so this x maps on to this x because they are the first, then the y maps on to the y because it's the second, up maps on to direction because it's the third and distance maps on to one because they're the fourth, right, so there are arguments that are passed by order. Now you are allowed also to refer to arguments by their name, so we can say for example distance is one and direction is up, that's fine. What's kind of cool is that once we start referring to them by name we can also swap their order, so we can say distance is one and direction is up. If I select this and I run it you will see that it still works, right, so even though we violated the order because we've passed distance before the direction whereas direction has been specified before distance in function, Python still figures out which argument belongs to which to what because we've actually called to refer to them by their name. Now we're not allowed to do just anything, for example all the arguments that we don't refer to by name x and y need to come before all the arguments that we do refer to by name, so distance and direction. So if we start to mix this up if we say for example x distance is one and y you see that it gives a syntax error and it says okay you're not allowed to do that. Once we start referring to it by name again then it's everything is fine, right, so if we do this the syntax error is gone. Okay, so these are arguments. Now let's say that distance especially in this case is usually one, right, usually we want to move by one, so what we can do is we can specify default value for distance, distance is one and once we've done that we call it a keyword, so the difference between what people tend to call arguments and keywords in Python is that keywords have a default value. Sometimes people also talk about default and non-default arguments but I prefer the term argument and keyword, so distance is a keyword here and x and y and direction are just arguments. Now the main advantage of providing it, of using keywords, providing a default value is that you can omit that argument when you call the function, right, so this is redundant, distance is one because we've already specified one as the default value here, right, so we can just remove it and then I call it and then it will know, Python will know that we want to move by one because we've specified that as a default value. Now and then only if we want to say for example direction is two or sort of distance is two because we want to move by two units, we can specify it, override the default value and now we move by two units, right, so that's how keywords work. Now and just as you need to, just as here when you call the function, also when you define a function all the arguments, so the arguments without a default value have to come before all the arguments with default value, right, so if we were to move distance to here, to this place, we would get a syntax error, that's not allowed because now we have argument argument keyword and then argument, right, so all the arguments need to come first and then all the keywords, so this is fine again. Okay, well that's basically all there is to say about arguments and keywords. Let's move on to a slightly, slightly more exotic and complicated way of using arguments, namely through argument lists. Now and to do that I will define a new function which I've called move more and move more just moves more, it makes moves by multiple steps, right, so here we have it, so what move more does is first it takes x and y coordinates of starting position and then it takes a list of directions, right, so we can move for example up, left, up, left, say and then what the function does is it loops through all the directions and then for each direction it makes one move and then it returns the final position, so if we want to call move more and we can do it like this, we say move more x and y coordinates and then we say okay we give a list of directions, so we can start for example, let's say just we're going to move by two steps up and right, okay, so we move by two steps, so x would go map onto this, y would map onto this and this whole list up and right would map onto directions, now if I select this and I run it you see it does as we expect, right, so we've moved up by one, so the y coordinate has increased by one and we've moved right, so the x coordinate is also increased by one, now what would be kind of nice, right, you see that the way we've the way we call this function is a little bit clunky because we have these square brackets, now what would be kind of nice is if we could simply do something like this, right, we could say okay x, y up and right, now the problem here is that we don't know, right, we want to make the function move more very flexible so that it can take an arbitrary number of moves and now basically the way we've done it now, you would think that we need to find first direction one and direction two etc, right, so we would need to make a move two function move three function etc and that's actually where argument lists come in because what we can do is we can do the following, we can start with an asterisk and then we can say directions and what this means is that basically the x still maps on to the x, the y maps on to the y and all the arguments that follow and that don't really map on to anything are stored in a list called directions in an argument list and that's specified by this single asterisk, right, so and then we can again walk through this list and move make well do as before right move make one move for every direction so if I select this and I run it you'll see that it works it just does exactly the same thing as before moves to one to one but now we don't need to explicitly say that these two items are a list we just pass them as normal arguments and because we have an argument list here in the function Python automatically captures essentially the up and the right into a single list called directions that's the logic behind an argument list it can be kind of convenient okay now you also see that the move more function is a little bit more restricted than the move function because it doesn't accept a distance keyword right whereas the move function does accept the distance keyword so what can we do we can say okay distance is one and okay and then we say distance is one oh sorry distance is distance right so we essentially we take this keyword argument and then we pass it on to to move right so if I select all of this and I run it you will see it still works I have to point out though that this is only valid Python syntax in Python 3 in Python 2 you are not allowed to to put keywords or arguments after an argument list so basically once you've done this you're not allowed in Python 2 to put any keywords behind it but in Python 3 you are right so what we're working with here is Python 3 now so that's kind of nice right because now we can do something like distance is to select everything run it and you see we moved to 2.2 so that's quite nice now we can make it even more fancy using a keyword dictionary and that works as follows so instead of specifying explicitly that we want we have a keyword distance we say okay keyword arcs for example the keyword arcs is kind of the default name and we prefix it by two asterisks so basically if you prefix it by one asterisks it becomes an argument list if you prefix it by two asterisks it becomes a keyword dictionary and then what we can do is to follow so and we can call it exactly in the same way as we do now so if I select it and this I should say is both valid in Python 2 and Python 3 so after a keyword after an argument list you're allowed to put a keyword dictionary in Python 2 and 3 of course now so what we're so what what happens here is essentially that all keywords so in this case distance just distance are captured in this keyword in a dictionary called keyword arcs and without knowing what this dictionary even is we just pass it blindly on to move right so maybe to make it more clear I can say okay let's print out keyword arcs and you will see if I select everything you will see that this print statement corresponds to this and you see that it's a dictionary with a key distance and a value 2 right so that comes from here so this keyword argument is transformed into a distance now and then we pass it again on to our move function where it is where it goes into here and that's how what that's how basically the distance is preserved with the distance keyword is preserved even though we have not explicitly defined a distance keyword here in move more right so let's say that we we could all say let's say that we for example pass keywords that don't exist so invalid is true for example if I select this and I run it okay move complains that it got an unexpected keyword argument called invalid and this happens because invalid is a keyword argument that is here captured in this keyword dictionary we pass this keyword dictionary on to move function and that basically means the same thing as calling the move function with an keyword argument named invalid well there is no keyword argument named invalid in the move function so we get a get a type error right it's not allowed now the main advantage of using keyword dictionary is if you have a function that calls another function and it's basically it should pass on a lot of the a lot of argument keywords to this new function say for example that you're drawing that you have a function that draws a line and that and that function takes a color for example line color it takes a pen with for how thick the line is it takes a line style maybe whether it should be dotted or dashed etc takes a whole bunch of keywords now and then you have a another function wrecked rectangle that simply draws four lines to create a rectangle now then you then then a keyword dictionary is very very convenient because you could basically the rectangle could simply capture all the keywords in a keyword dictionary and pass them on to to to the line function right so those kinds in those kinds of cases keyword dictionaries can be quite can be quite convenient now in general what would be my recommendation when using when using fun arguments keywords argument lists and keyword dictionaries well I would say that in general keywords are really your friend I think keywords are quite nice because they are named right so keyword like distance here are quite nice to use because they have a name which makes it much easier to work with right we tend people tend to mix up orders of arguments if they need to be passed by order so using keywords that have a name is generally more leads to more bug-free code I think so in many cases I would say prefer keywords over arguments without a name or arguments that are specified by order and argument lists and keyword dictionaries as you see here can be quite convenient but I would say use them only in very specific cases if you really know what you're doing because they can also lead to kind of confusing confusing code okay that's it thank you very much for your attention