Python Tutorial for Beginners 14 - Default parameters and Multiple Arguments in Python


hello guys in the last blog I have shown you how to use and how to define a function in Python in this blog also I am going to continue with functions and I am going to show you how to use some other features in functions like default parameter and multiple parameters in function so let's get started in the last blog I've shown you how you can declare a function so you can declare a function in Python like this def for example I want to make a function to print out the score of a student so I can say student poor for example this is name of my function and in this brackets I can pass parameters right for example name and score okay and then this colon and inside this function we can write the definition or the food to our function for example I want to print something print the student name and score I can just write the or something like names for this much amount of marks so I can just write name and then comma and then score and then comma this should be score right not scare from a Mark's right and this is simple you have already understood I think the concept of you know defining the function and how to print these values right and if I call this function it will just give me the answer for example a boss named as mark and score and 70% at sales mark scored this much amount of marks right but sometimes you want that there are some default value which are already initializing these parameters for example in the case of databases here it's a bad idea to save blank values or null values in the database so and you cannot depend upon the user that he can define or you cannot you know be sure that user will define a parameter for every function so what you can do in Python is you can define default parameter so once again I'm going to take this function but I will initialize this function with some value so that it will not be null at any point for example Tom as name and score is equal to zero for example right now I can just print the score and name and the default value will remain just the same right and my function is defined and now when I use this function and even if I don't pass any parameter generally you have to pass the parameter when you define a function and when you define a function with parameters but when you initialize a function with default value you even skip passing the values to your parameter and once I call this function it's a storm scored zero mark because Tom is a default value and score form is zero okay so Tom is a default name and zero is the default mark and so you can ask what is the purpose of this the purpose of this is if the user I will not pass any value as an argument then the fault value will be taken otherwise if the user enters some valid name for example mark and 85 marks and once again you can see it's printing mark scored 85 marks okay so it has overwritten the storm as mark the name it has overwritten as mark and this score is overwritten by 85 right now you can choose to you know pass the first parameter and skip the second parameter for example you can just pass mark here and it says marks code zero marks because this it has taken as the first parameter and it has over over it in the name but we haven't passed our score so it will take the score as default which is zero so it will print like this now for for example you want to pass this score and you want the name to be default what you can do once again take the function and this time just write score is equal to 99 for example it says Tom scored 99 marks right so Tom is the default value here and 99 is overwritten or zero is row overwritten by 99 which you passed okay so this is about default parameter passing now there is a concept of passing multiple parameters to function and how you can pass this multiple parameter for example you I will take the same example of student and you want to pass some parameters so I want to pass name of the student and I want to pass multiple took score of the student for example students as scored you know marks in language marks in programming marks in maths or marks and some other subject and you want to pass the parameters but you don't know the number of subject you know student is you know taking so what you can do in this condition you can just use the name as it is but for example you want to pass multiple scores you can just use Asterix and this Astrix says python that this parameter will be passed one as one parameter also or multiple parameters also so move on or more parameters so our s trick says to the python that this score can be multiple also or even it can be one also okay and it's saying invalid invalid syntax let's see what's the error yeah I have forgotten the DEF keyword to define a function so I will define a function depth and then we call this and once again I can print for example I will print first of all the name print name and then I will print scores okay and I will just call this function for example student and I will pass name as once again mark and scores for example he has taken a language course so he scored 55 marks in this language course he has taken maths course he is 477 marks he has taking programming course he scored 90 marks he has taken other courses physics 67 marks or multiple courses okay so this is a wild card kind of entry which says you can pass one or more parameters right and you see it has printed first of all the name of the student which is mark and then these these marks as a couple if you don't know what is a couple couple is just like a list but you cannot add it a list or you cannot even delete an element from a list you can just read this element from tuple right so tuple is just like a list but the only difference is you cannot modify or they eat any elements from this double so this is a tuple right so in this way you can even pass multiple values using this Asterix and it's not necessary that you need or to pass only two parameters you can just pass one parameter for example def score something like this a strike score it will be valid I have forgot this call : or like this and what's the problem here so okay I left some indent here which it's saying unexpected indentation okay and you want to print score and be totally valid when you call this function score and you can pass multiple values 1 comma 3 some some marks multiple marks right and it will give you our topple so in this way you can pass default parameters to a function and you can pass multiple parameters to a function in Python I hope you have enjoyed this blog 

Post a Comment

أحدث أقدم

Recent in Technology News