Transcript:
[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 Tuple’s 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.