Transcript for:
Notes on Python Arguments

[Music] welcome back aliens my name is 720 and let's give the CDs on Python now to this point we have talked about functions right and now we know how to pass parameters as well but when you talk about parameters all you can call them as arguments we have different types of arguments we can work with of course arguments arguments at the end but then we can we can using different way sort of talking about here is this I want to add two numbers a very simple example I would just want to add two numbers here so what I will do is I will say def I will get a method name as some of the function name as sum and here I will mention two variables of course right when you want to add two numbers we need to ask for two values right and we can give them name as a and B so in a will be having the first number and B will be having second number right finally in order to find that we have to put a colon at the end and you will say C equal to a plus B now you have a choice here you want to add when you want to print the value here itself all you want to return but that's not the main concern for this video so time then I will simply print it what is important here is when you call this function which is add you have to pass two values you cannot simply say add right of course you have to pass values so when you are accepting an arguments here you need to pass the argument as well otherwise it will give you error right you can see so when you press the control button and if you hold that it says we have to pass two parameters right now in that case if I pass five and six so we are passing two values right now these are for this argument so the things which are accepting here that there are arguments i1 this is an argument now this a here so this a B here they are called as a formal argument so we have two names here we have formal arguments and we have actual arguments so when you define it it is this formal and when you call when you pass the value when you are calling a thing which is called as actual argument because you're actually passing something right so this actual itself the actual arguments has four types but its position keyword default and variable length arguments so let's talk this first one against not that difficult it's just with different names okay so when you pass a bad example if I want to pass for you for this of course it is positioned right inside just for this example we will take a different names okay we will take a different method name I will say the function name is person and instead of talking about a person we that put in some very important here the first one is name and second one is eight so we want these two parameters and of course you have to pass them as well so if I change this let me exchange all the values here so I define the function which is personally takes two arguments one is name and age and from so I'm cutting that I'm calling person by passing name and age so name is Naveen and age 28 28 so if I am this code you can see we got the value it is working perfectly right now how do we know that this number is going in name and 28 is going in the age so that's why positions comes it comes into picture if you have 10 variables and you want to make sure that it is the values are assigned to the particular variable we have to take care of the position you cannot simply exchange the position okay so positions are very important here so when you are already depending on the positions then of course we have to go forward we have to maintain a sequence but what if I don't know the sequence so let's say I have defined that this function somewhere else okay I don't have the access to this function what I know is there's a person function which takes two parameters name and age but I'm not sure about the sequence in that case example if I forgot the sequence here and instead of having instead of passing Naveen Naveen first I'm passing 28 and then I'm passing Naveen this is where the problem starts you know because we are passing a number first and then we are passing a string of course in this case you can work it will not give you any error of course you will get the output in a different format the part of area of passing but let's say if you are performing some operations on age I want to print I want to print the age minus 5 that's what we all do right so whatever our age is we always say minus 5 with that and if I run this code you can see this is where problem starts because you cannot simply be subtract a number by a name because so nobody is getting passed in age right that's what the problem is I don't do that I don't want to assign the value 28 to name and naveen to age in that case if you don't know the sequence you can simply say hey a is equal to 28 and name equal to Naveen so in this case we are using keyboards right so when you say key what it means you are mentioning the variable name itself so variable is age which is 28 and variable is name which is naveen in this case it could work okay you can see we got Naveen and 28 so this is the keyword arguments the next argument is default know what it means example I want to pass a name and I don't want to pass an age because by default like let's say if you want to open an account in social networking website like Facebook maybe there's a there's a criteria that you should be 18 to join a facebook profile now in this case if I if I pass a age that's well and good if I'm passing an age which is 28 that is well and good well what if I don't pass the age so in that case what I want is by default the age should be 18 so if I'm not passing the age which means it should be 18 but in this case if I love this quote it will give you error because you are expecting two variables and we are passing only one this is wrong right so in this case what you can do is you can mention here age don't feel bad while calling a function if you're not getting the value don't worry just assign the default graduate is completely fine just run this code and it was okay so if you're not passing a value it will take the default value but what if I pass the value in that case it will override the existing default value so you can see in this time it was 28 that's default so once you have talked about default keyword and position we let's go with very important function arguments which is variable length arguments know what that means to understand this variable length argument concept I will change the function again let's get back to some because it will make much more sense there of course you can take different example but then let let me lick some there or we can say add function but let's say some some takes two parameters a comma B and here I just want to add those two values I will say C equal to a plus B and I just want to add them now I want to pass 5 comma 6 and of course I missed one thing I want to print this sum which is C okay now will this work let's verify so if I say shift f10 it works you can see we got 11 so when you say you want to add two numbers right see we don't always add two numbers maybe sometimes we want to add three numbers maybe you want to add five numbers because in capital we can do that right we can say 2 plus 3 and then you also say plus 4 plus 5 plus 6 ten plus hundred we can do that how could I pass multiple values here so maybe as a user you want to say hey I'm not sure how many values I want to add maybe I will pass two more values you should be able to do that okay so as a developer is possibility right if you're defining a function make sure your function accepts multiple values then what you will say hey we will create multiple functions or no we create multiple functions with different parameters or different arguments we can do that but the thing is don't you think it will be very very very lengthy process so that's where we have our variable length argument concert what it means is you can define a function where the number of arguments are not fixed so in this case you can say hey at least we have one parameter right at least we'll be having one argument of course if you want to have two numbers we will be having at least two arguments so the first one is spent formed the second one length is not transformed so then this B I might accept one variable or one value or multiple values in that case you will say star B star B star B means it can have multiple values so this five will be assigned to this a and this 634 and 78 will be assigned to be okay and as a tuple now let's run this code let's verify okay we got an error the error is see there is no error bike while calling it the error is with the help of int and tuples right so we cannot simply add an integer and a tuple because a is integer and B is tuple again how do we add this value that's a question right so first of all before before adding this values whatever I'll do is I will print the value of a just to verify what is happening and I want to print the value of B just to verify let's run this code and you can see we got five and we got a tuple which is 6 36 34 and 78 so it perfectly makes sense right now how do you add this value of course the first one is fixed so what we can do is we can say C is equal to a initially C will have the value of a and then we'll fetch one one value from the tuple and add it to C right that's the plan so I will take for I in my B which is tuple and in this case I will say C is equal to C plus I right that's how you create so every time your loop runs it will add the new value to C and at the end will print the value of C that's what that's the complete idea let's run this code and doubt we got an error okay that was an indentation problem you know so I was given one extra space and you can see it works we got 123 times what we wanted right so this works great in fact we can do some customization here what if I say I will simply mention be star okay so it doesn't matter how many value you pass why do we even need Adrienne right so if I pass four values everything will be dead in B so in that case initially you will say C equal to zero that's the only change you have to make and everything works the way it is right so because you're not passing a so we are not using a so that's the move a right so that's it that's how we use a variable and argument so in fact it's not just integers you can pass integer string or float together that's fine you can try it out and let me know the let me know your question in the comment section so that's it we have talked about different types of arguments in this video we have talked about default position keyword and variable and arguments so I hope you enjoy my sections reppin in the comment section thanks for watching bye