Python Tutorial for Beginners 20 - Default Arguments, *args and **kwargs (Variable-length Arguments)Python Tutorial for Beginners 20 - Default Arguments, *args and **kwargs (Variable-length Arguments)


 hey guys welcome to the next blog on Python tutorial for beginners in the last blog we have seen how to declare functions and how to use functions in Python in this blog also we will discuss some more properties which are related to functions in Python so let's get started so to start with I have a function called student which takes two arguments one is name and other is age and inside the function we just simply print the value of name and age using the print function and when we call this function with the name and age it's going to print this kind of output now Python allows us to set the default value of the arguments so after this name you can put equals to symbol and then put the default value whatever you want to set for the name for example if somebody doesn't provide any name then you can just say unknown name similarly we can set the default age here let's say default age is 0 if somebody doesn't provide any age with the student function then the default value will be 0 now instead of using this student function like this let's say I don't provide any argument to this student function and let's run the code and you can see if I don't provide any argument to this student function the default values are taken so the default values are used when you want to make sure that every argument in your function should have some value now let's say I just provide a name here and don't provide the age and let's run the program and now you can see the name is overwritten by the argument what we have provided here right so default value will be this one if no argument is provided see if we give the value of age and now we run the program the default age is replaced by the age which is provided using the arguments so this is how you can use default values with the arguments of the function so let me just remove these default values and I'm going to give you the next example which is variable length arguments so let's say with the name and age I want to provide the list of scores which this student has scored for different subjects so as a third argument I'm going to provide argument called marks and somehow I want to provide a list of marks which is scored by this student and let me print the value of marks using this print function also now in Python you can use Asterix in front of your argument and this means that you can provide multiple arguments when you use this kind of notation so let's just provide for example we already provided the name and age here and let's provide different marks scored by this student so I can provide 95 for first subject then 70 for second subject than 80 for next subject than 50 for next subject and let's say we don't know how many subjects the student has taken we just have only the information about these four subjects which he has taken so we can provide the four values here and then let's run the code and now you can see name is Tom age is 22 and the value of marks is shown as a tuple okay so whenever you provide this Asterix in front of an argument you can provide the values for that argument using normal arguments as you provide for normal argument so first two variables are associated with name and age and all the other variables which you provide here will be assigned to this marks variable because it has this asterisk in front of it so now you can also use a for loop in order to iterate over tuples so X in marks for example and then you can print every value or you can access every value inside this tuple and let me comment this print function and let's run the code once again and you can see for loop has printed all these marks which are there inside the list now if you want to use this type of variable length argument then I will suggest you to use this argument as the last argument of your function so that it will be clearer to the reader of the function that whatever you provide at the end will be the part of this last argument now you may ask that we have provided these marks for this student but for which subject these marks are given to the student now in Python you can also use double as tricks in front of your function argument and this means that now you can provide the key value pairs as a marks arguments so instead of using 95 I can also say that 95 is code in English let's say and then 70 is code in math and then let's say 80 is scored in physics and the 50 marks is code in biology so now when you use these double asterisks in front of your function argument you can provide these kind of key value pairs which are separated by this equal to symbol and how you can access these values so let's run the code and let's see what happens and now you can see only keys are printed and not the values of these subjects are printed so let's first uncomment this print function and let's see what type of value getting so whenever you use these SS tricks the values are given to you in the form of our dictionary okay so if you use single asterisks the values are given to you in the form of tupple but whenever you use these SS tricks and whenever you try to access this variable then this variable will be of type dictionary and you already know how to print the values of dictionary so you can just write marks dot a terms here and then here you can just write key comma value and then you can print the value of e and value so I'm going to just print key first of all and then I am going to just give a space here and then I'm going to print the value right and now when we run the code it will give me first of all the key for example English math physics or biology and the value which is 95 70 80 or 50 so I think that's enough for this blog please keep reading these blogd and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News