Day:7 Functions and Parameters In Python | Live Lecture

Akansha Yadav covers Python functions, parameters, return statements, and scope. She walks through recursion with factorial and Fibonacci, shows how to modify global variables inside functions, and refactors a calculator into function-based code. Practice tasks include printing the first ten even numbers and generating multiplication tables.
Chapters
Hello everyone, how are you all? Yeah, am I audible to you? All right, so let's begin with today's topic, and our today's topic is functions and parameters. Let me know once my screen is visible to you all. Is it visible? Okay, so our next topic is this functions and parameters. So now when we are writing the codes and when we wanted to write certain codes multiple times, or maybe we wanted to use a piece of code again and again, so we can use functions there. So what I mean is, let's say I have written a program that I wanted to add two numbers. So simply I have written I wanted to add a b, but that a b I wanted to perform multiple times. Maybe right now I wanted to use it, or maybe after writing few lines of code again I wanted to do addition, and after certain like execution I again wanted to do it. Basically I wanted to reuse that addition one. So whenever you wanted to reuse any specific code, any specific piece of code, you can add it as a function. So functions is a block which will help you reuse your code which is related to the specific task, and it is going to reduce the redundancy as well, and it's going to enhance the modularity. When it comes to modularity, it means you can modify it Like, as I said, a b— when it comes to a b, it means I am not giving it a direct number. I'm not saying 5 5, I'm saying a b. So a can be any number and b can also be any number. We are not defining the numbers right away, we are just mentioning a b. So we can change the value of a and b whenever it will be required, and it is also going to improve the readability of your code. So using function, you can do all of these. So for example, like in the previous session, we created one program which was the simple calculator. So I'm going to take this entire program and I will show you how functions will change this calculator. So I'm going to paste it over here. Let me just change the cell to code cell. And this one I'm going to make— okay, now it's great. Okay, so here we were trying to do some addition, here we were trying to do subtraction, then multiplication, right? So I'm going to use functions for all these small tasks, and I will modify the simple calculator. So first, let's understand the syntax of function. That how exactly you have to define any function. So when you wanted to define a function, there is a keyword in Python which is def. Def stands for define. So you have to write def and then you will write function. Def is a keyword which is used to define function. Following with the def, you have to write function name and in the bracket you have to mention the parameters. And you have to add a colon. Again, the concept, same concept, indentation has to be there. So these spaces are mandatory. So you have to write def, then function name, then in the brackets you have to use any parameters. Parameters are nothing, just a variable. And then you can write down whatever, like if you want print, you want any calculation, you can write it over here directly. And Then there comes one more parameter, which is one more thing, which is return. And then you can return any expression from this. This is the exact syntax that you need to follow for functions, and this includes everything. There are multiple types of functions basically in Python. So some are built-in function, just like we were using print. So print is built-in function. When we were using type, type is also built-in function. You remember those, uh, like we were writing type, and then we have also learned about print. So these two are the built-in function which we have covered so far. Same way, now the next type of function is user-defined function. User-defined function is this one which we are learning. So this is user-defined function. These are already defined in Python, so that is why we call it built-in function. These are the functions which we are defining on the basis of the task that we wanted to achieve. So this is user-defined function. Within user-defined function, you have to pass parameters. Parameters are nothing just a value or inputs for the function, like the numbers. It could be, it could be message, anything that your function requires to work. Functions can take input which we call as a parameter, which you can customize And, uh, with the function, like, with the function, when you're writing the parameter, right, you cannot limit yourself because there can be multiple parameters in the function. So let's first define a simple basic function which will help us to add certain values. So I'm writing def, let's say add is my function name, and a, b, which is for addition. I need two numbers, a and b. You can name it as number 2 and number 1 as well. So this is also a good way. And then you can simply say return number 1 plus number 2. I'm really sorry. So def add number 1 comma number 2. Now I wanted to add 2 numbers. I'm not passing the direct numbers, number 1 and number 2, because number 1 and number 2 can be anything. Return function. This return is basically used when you wanted to return something from the function, when you wanted to return any expression from the function. So when you wanted to return anything like calculation related to calculation or related to result from the function, you have to use this return keyword. If you are going to write down the return keyword, then you need not to use any specifically like any print or something to display the result. Directly return will help you get the results from the function whenever we define any function. So this is done, like I have created a function which is add. My function name is add and my function accepts 2 numbers. When I have written the parameters number1, number2, it means my function accepts 2 parameters. If you are going to call this function— yes, that's a point that when you are defining the function, you have to call the function also. If you are going to call it, then only your function will be active, then you will get the results. Otherwise you won't get the result. So for example, I wanted to add two numbers, so I'm writing add, and let's say my two numbers are 5 comma 10. These are my two numbers. So I can simply run this and you can see that add bracket 5 comma 10 is giving me 15 because This is the function which I have defined, and I am using the plus operator in this, and we are getting the addition. Now let's say my function name is add, but in the results I am writing multiply. So you will get the multiplication because this is user-defined function. It depends upon you how you wanted to make it, how you wanted to make it function. Okay, so you have to use the correct syntax. Def is the keyword, this is the name of the function, these are the parameters. Then the return will help you get the multiplication. If let's say I wanted to display something, so I can use the print statement. Wait, I have it— the addition is— and I can run this. Now you can see from the function itself I'm getting that print, and I'm calling this function. This is the function call. So to activate that function, you have to call the function. To activate it, to make it work, to check your function whether your function is giving the correct output or not, you have to call the function. You can call the function n number of times. So basically, I can do addition with multiple numbers. So let's say 98 comma 15. So now you can see the addition is this. We are using multiplication operator, that is why we are getting those kind of results. But if you are going to use the correct operator, you will get the addition. So if I want it to use again, I can use it, and you can see that I am getting the additions directly. And I have also written the direct print statement to get the result, therefore we are getting it. Now we are getting the results in the next line because this is the built-in thing, like this is from the syntax of function that we are writing return and we are asking it to return the expression. That is why we are getting it. If you want it there itself, so you can write it over here, num1 plus num2. So you can see the addition is 15, the addition is this, the addition is this. So you are getting it in the correct format as well. If it is related to just the value, then you can write it like this. But if suppose after function, or you are going to use this function to perform something later on, like maybe if I have written this, I'm going to write it within print, let's say add these two numbers and then add 10. So add function I'm calling within print and I'm asking it to add 10 more in it. So when I'm saying add this, this plus 10, it means this is the output which you are getting. Which is 123, because in 113 we have added 10 and we are getting 123. So when you want to do further calculation on the value generated by your function, then you need return. Return is must. I can write down this function with this 2 lines only. Okay, and let's say now add 2. Okay, this is correct. We are able to run this as well. Now I'm saying add_to bracket begins 10 comma 15. So now you can see it says that the addition is 25. Now I'm going to write down print add_to bracket 10 comma 15 plus 10. We are getting some error. Because we are not using any return, that is why we are not able to perform any further calculation on the value generated by the function, because the value which is generated or which is calculated within the function is in the function only. After that, if you wanted to calculate anything, you have to use return. In case you are not using it, you will get this TypeError: unsupported operand types for plus NoneType and integer. Because there is no data type, because this is generated by the function and this is the integer. So we cannot perform any addition on the None type plus integer. That is what you are getting in the error. So return is must when you wanted to use the value later on. So I would prefer that you use return. Okay, both functions are behaving same, both functions are going to give you the same results. Only the difference is we are using return and we have written another function without return. So without return, you won't be able to perform further calculation. Any question up till this? Anyone, any question? Is this clear to everyone? Now when we— when I said that we are going to make modification in this, so simply You can— let's say I'm going to copy this section, add it up over here, and then you can start creating functions like add, let's say, a comma b, and then let's say return a b, and then def subtract , and then you can create multiple functions for all those operations, and whenever it is required, you can directly call the function. This way you will be able to Write down the code and you can reuse it. So return a multiply by b. And the last one is the division. a comma b return a divided by b. So now we have defined all the functions required for this basic calculation, basic calculator. Then what we have to do is we have to take the input and same thing we can copy. And then I will just write— instead of just writing this entire thing, we can just say like this. You can modify your code. Or if you wanted to use the same statements, you need not to use this. You can replace it over here. You can just call a function directly. Got it? This is how you can use functions, and this is how you can increase the reusability of your code. The main thing in functions is the parameters. Okay, so these parameters are important. So let's say you have defined this function add. If I'm going to just say add comma 5, if I will just provide one value, it will give you an error saying that one missing positional argument. It means this also requires the second value, which is num2. So you have to provide two values because you have created a function with two parameters. And when you are creating with the two, you have to call your function also with two variables. So the way that we have passed the parameter is called positional way. Like, this is the positional parameter. Or you can call it position parameter, okay, positional parameter. This is common one, and like, this is very direct way of writing a parameter. We also have a default parameter. Let's say you want that whenever this num2 is like— if I'm not passing any num2 value, I want that if the num2 is not passed, that assume that this is 50. So now if I'm going to just write 5 I have to execute that. Now if I'm not writing the second value, then it will accept because this is the default value which I have provided. So this is called default parameter. It means I have provided a default value. Whenever there is no value passed or no argument passed, then at that time that default value will be used. Any question in this? Um, if we pass like a 5, 6, then the second parameter 6 it will take, or 50 it will take? It will take 6. Good question. If we are passing the default value, default means if none, if, if the value is not passed, then you have to use it. Okay, if it is passed, then you have to accept the latest value. Okay, okay. So simply, default means if value is not passed, then to use. Otherwise, whatever value you're passing, you have to use. Anyone else? Any other question in this? Now let's say I'm not sure how many parameters my function will require. Okay, or maybe I'm not sure, like I wanted to do addition between 2 numbers, 3 numbers, 4 numbers, or how many numbers exactly. When you are not sure about the parameter, then you can use the arbitrary positional parameter. Arbitrary positional parameters are like a, a way that allows you to pass as many variables, as many parameters you want. So let's say I'm writing def summarize bracket. This is how you have to write arbitrary parameter. So this is arbitrary argument. So asterisk symbol is used to write arbitrary positional argument. You have to write asterisk symbol and a-g-r— a-r-g-s, sorry. And then you can follow up with the next command. So let's say I wanted to say return— I'm using the built-in function sum. There is a built-in function in Python, sum, which will help you perform the addition between the numbers. So I'm going to run this. Now I'm going to call this function. So my function is summarize, and let's say I wanted to write 1, 2, 3, 5, 7, 68. And this will do addition of all the numbers. So when I'm writing this arbitrary positional argument, then I can pass as many value as I wanted to. This is arbitrary positional argument. It allows variable number of positional argument. When you are calling the function, you can write as many values you want to. Got it? Anyone, any question in this? Just like I told you, the use of return, same way variables are also, uh, like it plays a role in the function, like they have different scopes. So variables are basically of two types. One is the global variable, second is the local variable. Global variable means those variable which can be accessed anywhere in your Python notebook, Python file. Those are global variable, as the name says, that those variables which can be accessed globally. Second is local variable. So local variables are those variables which are defined where— like, wherever it is defined, it can be accessed only within it. So let's say if I'm not passing any parameter or nothing, I'm just keeping this black bracket blank. So let's say def example bracket blank, okay? And I'm just going to say local variable is equal to 0. Okay, so simply I have created an example function where I'm writing local variables as 10, and I'm going to print the value of local variable from the function. Okay, and I'm going to run this. So I have created one function. Now I'm going to call this function. Whenever I will call this function, it is going to show me the function, like the detail about it, right? So this is a fun— sorry, I have to use this. If you are writing the, like, with the bracket, you will get the print statement value. So print statement value says that the variable, local variable value is 0. If I am storing 10, then it will give 10 as a value, correct? If I am writing the local variable value then you will get the local variable value with the sentence, correct? But if I am going to write down— obviously local variable is the variable itself, this is the name which I have given it— and if I'm going to say print local variable, then it will give you name error because local variable is the local variable which can be accessed in the function only. It cannot be accessed anywhere in your Python. So it has a limited scope. So the variable which you have defined within the function, that can be accessed within the function only. Got it? If I'm going to write down the same function Let's see, let's add it over here. Let's say I'm writing— I'm just changing this variable. Enter. Instead of local, I'm going to give it a name global. Okay, global var is 10, and I am going to say example. Example we have already written. Let's just change this as so to— now if I'm going to call my example2 function, we are able to get the value of global variable. So this is global variable which can be accessed anywhere in your Python notebook, whether it is within function, outside of function, anywhere. Like if I wanted to call it over here, obviously we will get the value because this is the normal variable. Now you can see, well, this is global and this is the local value. Any question? Now let's say, obviously in functions sometimes we have to define variables, and there is no point of defining a variable which is a local variable. There are some situations, there are some scenarios where we can define it and where we have to define the temporary variable also. But what if we want to make a variable, a global variable? So when you wanted to make a variable, local variable, a global variable within a function, then you have to add a word global before that variable. So let's take an example. If you wanted to make it global, write global and give it a space. Like, this is how it should be. And you have to write it like this: global this value, and then after that you can say local var is equal to 10. So this way what you have done is you are getting the value. Let's say I'm just going to update it to 2. So this is 20, which is the local variable value, and I'm going to write down print bracket local value. Now you will be able to get the value because we have converted that local variable into the global variable just by writing a global keyword. So you can simply modify a variable inside a function with a global word, and that will be global value. Any question in this? No, there are some cases when you wanted to call a function within the function itself. So whenever we are calling a function within the function itself, we call it recursion. Okay, so recursion is calling a function within a function, and you can solve multiple problems if you are going to do this. Okay. You might have learned about factorials in your school or college time, or Fibonacci series. These kind of problems, if you wanted to solve with programming, you have to use the concept recursion. So recursion is calling the function within the function. So wherever you wanted to call a function, you have to use that— you have to call it with the same name and same number of parameters. Now let's try something with the string data type. So functions are not just for the numeric data types, so functions can use string data type as well. So let's say I wanted to print the name of the person along with hello. So simply we will create a function def display name. Let's— I'm writing name print. Okay, I'm going to use return just to show you that return can be used for this type of successful— I wanted to use Hello. And then so I want hello along with the name to be printed in the output. Now I'm going to call this function display_name bracket. Let's say I'm writing hello Python, so it will display hello Python. If you wanted to add something in it and you wanted to like make it with the print statement also, so you can just simply copy this and add it in the print statement. This way you will get it with the— within the print statement. So functions can be used for any type, anything. Like related to numeric, or it is related to sequence, anything. Okay, any question? Anyone return? We have to write in this also, means, uh, in function we have to write return. It's mandatory, right? It is advised that you write return. If you're not going to write return, nothing will happen. It will give you the, like, same result. Okay, the only difference that it makes is you won't be able to perform something further on the value generated by the function. Just like I have given you an example over here that you have— if you are writing a function without return, right? So let's say this function is generating the addition value. So you want to be able to perform anything on that value. Let's say the value is 25. Now you want to add or multiply or divide or something like any calculation you wanted to perform on this number. You want to be able to do it. If you will try to do it, you will encounter this TypeError. And when you are getting this TypeError, it means that this is NoneType and integer. Now it means you have not written return, you have not used return in your function. So you have to go back to your function and add a return in it. And then you will be able to achieve this. Okay, ma'am. Yeah. Anyone else? Any other question in this? Yeah, anyone else? Any other question? Also, have you guys tried to create any type of calculator using the— calculator using the concepts that we have covered? Okay, there's a quick question for everyone. Now you have to write down— this is going to be a short code, okay? So you have to write down a code where you have to display even numbers. You can use any concept so far, like we have covered so many concepts like variable, data type, input/output operator, conditional statement, loops, functions. So out of these 7 or 6 topics which I have just mentioned, you have to use any concept and you have to try to print the even numbers, okay? And try to make it within 2 lines. You have to just write the 2 lines code and you have to print first 10 even numbers. Did you get the question? Yeah, if anyone have any question, you can just let me know. Manik, you have given the, like, answer in the chat, right? So what do you think, will it print first 10 numbers? Yes, correct. It will not print all the 10 numbers, so you have to write a logic that will print First 10 numbers, 10 even numbers. For i in range 1 to 11. If i is divided by 2, give 0. Okay, good, Keshav. So with the range function also you can do it. As I said that you have to use 2 lines of code only. With the number 11, you won't get 10 numbers. First 10 even numbers you are supposed to print, Keshav. So this logic you need to change a bit because you are only getting 5 numbers. Uh, it means like a 1 to 10 we need to do. No, we have to print even numbers, first 10 even numbers. Okay, first 10 even numbers. Okay, okay. And the same output you will get with this code also, which is 2 lines of code. Is correct, but again, logic needs to be changed because we want first 10 even numbers. Okay, so with range function you can do it within 2 lines of code. This way is also correct, this is logically perfect, but only the thing is you have to use this correct one, the correct number. Okay. For i in range and print i. Ruchi, for this you have to correct this p of print, and for f you need to correct because these are the keywords and you have to use it as a keywords. So now it's correct. Now your task is that you try to create one logic, okay? And that has to be that you have to display multiplication table. You remember that multiplication table that we have studied in school time? You have to display multiplication tables of first 10 numbers. So 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. These numbers, and you have to display it in this manner, like 1 multiplied by 1 is equal to 1, and 1 multiplied by 2 is equal to 2. Like this it should be, like the correct format should be this. Like instead of asterisk, use this X to make it more meaningful. Same way you have to go with the 2. 2 multiplied by 1 is equal to 2. Then same, 2 multiplied by 2 will give you 4. This is how you have to display all 10 numbers multiplication table in one go. It shouldn't be that you are going to take the numbers and you are going to display it. No, it should be within one go. No matters horizontally or vertically, you have to think about the logic that how you can write. Okay, otherwise we'll discuss it in the next session. Okay, anyone, any question? Ma'am, with function, how we solve this problem? The— okay, with loop one. Okay, which one you're saying? The even number one, right? Yeah, even number. Yeah, yeah, with for loop only. As I said, you have to use any concept. If you wanted to use the function, then simply like function is for reusability basically, okay? So you can add it as a function and whenever you wanted to display the even numbers, only one with the one single line of code you can make it print at that time, right? Okay, yeah. For today, your today's task is that you have to do this. You have to create multiplication table program which will print first 10 numbers multiplication table in this manner. This is the format that I am looking forward for. Okay. Somya, you have given i is equal to 1 and you are taking the value of x and you are making an increment in 10. But what do you think, will it print all 10 numbers? Result? Yes, ma'am. Have you tried? Yes, ma'am, tried. So like, you are giving it a number, right? Let's say I'm giving it 2, so it will display that multiplication table of 2, correct? If I will run this program again and I will write 5, so it will display the multiplication table of 5. But what I want is I want you to display without giving any input, it should display all 10 numbers table, like 1, 2, 3, 4. I don't want it to go with the user response. Okay, I just want, if I'm going to execute it, it should directly display first 10 numbers multiplication table on my screen. Now, got it? Okay, yeah, just try that. Okay, and then we'll discuss it in the next session. Till then, just try to practice these concepts. Okay, all right guys, bye-bye.










