1 00:00:00,330 --> 00:00:06,060 We're starting with the very important concept that is a Biton functions how to write functions in our 2 00:00:06,090 --> 00:00:06,960 python programming. 3 00:00:07,350 --> 00:00:14,790 Whenever we write the long code, then also we try to divide that code, segregate that code into multiple 4 00:00:14,790 --> 00:00:16,530 different models and functions. 5 00:00:16,800 --> 00:00:22,350 And whenever functions are there, then it will make us easier to understand that program. 6 00:00:22,560 --> 00:00:30,930 And obviously the reusability of the code can be enhanced by writing one related one function containing 7 00:00:30,930 --> 00:00:37,690 related statements which are executed as a whole whenever such an operation will be asked for. 8 00:00:38,220 --> 00:00:42,450 So let us go for the further discussion on this function of in Python. 9 00:00:43,980 --> 00:00:51,360 So function is a group of predicate statements that perform a specific task in our program function 10 00:00:51,360 --> 00:00:57,810 implementation breaks up a program into smaller and modular chunks and function helps us to make our 11 00:00:57,810 --> 00:01:04,770 program more modular, organized, easy to debug, reusability will be enhanced and so on. 12 00:01:06,030 --> 00:01:13,290 It is really features helps us to avoid repetition of our programming code, syntax of a passive function, 13 00:01:13,290 --> 00:01:17,390 how to write one function in our python so def will be written. 14 00:01:17,400 --> 00:01:22,960 That is a different function, so defined function also we can define so def will be there. 15 00:01:23,220 --> 00:01:24,750 So this is a function name. 16 00:01:26,040 --> 00:01:31,050 These are the list of parameters you may or may not pass, obviously, so list of parameters added. 17 00:01:31,440 --> 00:01:35,790 Don't forget to give the school on because if you get this call, that means you are starting with the 18 00:01:35,790 --> 00:01:39,770 function body and the function body will have that indent in that case. 19 00:01:40,260 --> 00:01:45,640 And there is a string which has been enclosed within three skorts the double there. 20 00:01:45,840 --> 00:01:51,330 So there is a GOC string doxxing if you want to insert the doctrine that is that documentation string, 21 00:01:51,450 --> 00:01:54,180 that should be the first statement within the function body. 22 00:01:54,720 --> 00:02:00,960 And then we can write one or more than one statement and then return value return and then return value 23 00:02:00,970 --> 00:02:01,430 would be there. 24 00:02:01,560 --> 00:02:02,760 And that is optional. 25 00:02:02,760 --> 00:02:05,110 A function may or may not return a value. 26 00:02:05,550 --> 00:02:09,300 So there is a way to write the function in about Biton. 27 00:02:11,000 --> 00:02:17,090 Types of functions, basically functions can be divided into the following two types, the first one 28 00:02:17,090 --> 00:02:23,210 is the building functions, the functions that are built into the python as an example, ebb's, which 29 00:02:23,210 --> 00:02:29,220 will give you the absolute value of the input parameter and then character with having to see each other. 30 00:02:29,390 --> 00:02:32,220 So there is a flow, there is a conversion to the float. 31 00:02:32,420 --> 00:02:37,430 That is, the print will be using this print, whether using this print multiple times in our python 32 00:02:37,430 --> 00:02:40,740 code to print some string on the screen, on the console. 33 00:02:41,090 --> 00:02:42,850 So these are the inbuilt functions. 34 00:02:42,860 --> 00:02:47,290 There is a built in functions we need not to write that is code for all these functions there. 35 00:02:47,540 --> 00:02:52,060 But I can readily use them in our code as when required. 36 00:02:52,490 --> 00:02:55,910 So next one is our use, the different functions and these functions. 37 00:02:55,910 --> 00:03:02,990 We are responsible to write the body and the function defined by the users themselves and to do certain 38 00:03:02,990 --> 00:03:04,160 specific tasks. 39 00:03:04,370 --> 00:03:11,930 All of us remember the function name should be very much, very much important because it is a different 40 00:03:11,930 --> 00:03:13,400 function from the function name. 41 00:03:13,550 --> 00:03:15,250 Who should get the purpose of the function. 42 00:03:15,470 --> 00:03:20,780 If you give the function name like function one function to function three, they don't carry an instance 43 00:03:20,780 --> 00:03:21,340 in future. 44 00:03:21,530 --> 00:03:26,360 So next time, if you if I ask you what is the purpose of function, one, you are supposed to look 45 00:03:26,360 --> 00:03:31,670 at the code, but the function name should be pretty much logical, very much meaningful, so that from 46 00:03:31,670 --> 00:03:34,910 the function name event you can get the purpose of the function. 47 00:03:35,330 --> 00:03:41,090 I let us go for one practical demonstration to show you how to write these functions in our Python programming 48 00:03:41,360 --> 00:03:42,910 with some simple coding. 49 00:03:43,010 --> 00:03:45,290 So here is the demonstration in. 50 00:03:45,860 --> 00:03:51,860 We are going to discuss functions and in case of Python functions, the function names should be defined 51 00:03:51,860 --> 00:03:53,690 and here will be using the keyword. 52 00:03:54,020 --> 00:04:00,160 There is a defined that is a diff and the function name within the opening and closing prospect. 53 00:04:00,260 --> 00:04:05,040 We can write the respective parameters if I want to pass some input parameters there. 54 00:04:05,720 --> 00:04:09,680 This is known as it was a different function because we are writing their respective function, but 55 00:04:10,440 --> 00:04:17,420 also function should be should have this opening and closing bracket that will indicate that it is a 56 00:04:17,420 --> 00:04:20,510 function here as here we are writing the function body. 57 00:04:20,510 --> 00:04:22,670 So it will be known as the user defined function. 58 00:04:22,970 --> 00:04:27,980 But if you don't write the function body, that means in case of print, you can easily find that this 59 00:04:27,980 --> 00:04:32,930 print is a function because it is having this opening our first record and the closing possibilities 60 00:04:32,990 --> 00:04:35,270 and it is taking a string as input parameter. 61 00:04:35,600 --> 00:04:37,970 But we are not writing the body of the screen. 62 00:04:38,180 --> 00:04:41,480 So that is why it is called library function or inbuilt function. 63 00:04:41,900 --> 00:04:46,910 After writing this def and then function, then opening back at closing bracket, then we shall put 64 00:04:46,910 --> 00:04:48,110 one colon here. 65 00:04:48,560 --> 00:04:53,660 Then this is part of the function body will have the indentation, which is the four blank spaces are 66 00:04:53,670 --> 00:04:53,930 there. 67 00:04:54,350 --> 00:04:59,360 So all the statements which will be maintaining this for blank spaces beforehand will be treated as 68 00:04:59,360 --> 00:05:00,950 a part of the function body. 69 00:05:01,510 --> 00:05:06,130 OK, so in this way that is no big and there is no culebras open. 70 00:05:06,150 --> 00:05:10,160 Culebras close, there is no block begin and block end marker. 71 00:05:10,400 --> 00:05:15,440 Only this indentation will indicate that these statements are within this function bodies. 72 00:05:15,920 --> 00:05:20,120 So this is this one is a function which is having no return statement. 73 00:05:20,120 --> 00:05:25,970 You can find that there is no written statement we have written, but in return we are having one return 74 00:05:25,970 --> 00:05:33,110 statement and it is having this string to be returned, return common returns, back the control to 75 00:05:33,110 --> 00:05:37,810 the column, and mainly used to return a value to the column of the function. 76 00:05:38,240 --> 00:05:43,190 So whenever we are calling this one, then this print statement will get executed because this is the 77 00:05:43,190 --> 00:05:46,220 only one statement within this one function body. 78 00:05:46,760 --> 00:05:52,430 But in case of Halo two, when we'll be calling this, it will return one string so the content will 79 00:05:52,430 --> 00:05:58,220 come back to the caller when this return statement will be executed and then print will print that this 80 00:05:58,220 --> 00:06:02,780 particular print function of print method will print the string, which will be returned here. 81 00:06:02,990 --> 00:06:04,490 So let us go for the execution. 82 00:06:04,910 --> 00:06:09,530 You can find this is the outcome of hello one hello, one function calling. 83 00:06:09,770 --> 00:06:17,660 And this outcome of this print has to function so a function can have the first step in which will be 84 00:06:17,660 --> 00:06:25,370 in the indentation will be known as the document string or string if it is enclosed within our triple 85 00:06:25,370 --> 00:06:27,550 single course or triple double code. 86 00:06:27,590 --> 00:06:29,830 So I can write this one as and this one. 87 00:06:29,840 --> 00:06:31,460 So this is. 88 00:06:32,180 --> 00:06:33,230 This is a. 89 00:06:35,650 --> 00:06:40,910 Multi-line text, so I've written this one within tripple single quotes. 90 00:06:41,440 --> 00:06:48,370 Now this is known as the dark string, and this docketing is a first step in this function body, which 91 00:06:48,370 --> 00:06:50,430 is having that in the indentation. 92 00:06:50,710 --> 00:06:54,790 OK, so how to access this one so we can do this one in this way? 93 00:06:57,730 --> 00:07:05,980 So in this way, I can access this tape and string or documentary, it can easily find this is in my 94 00:07:06,280 --> 00:07:14,610 line text instead of enclosing it within a single triple single cause, I also can use them as double 95 00:07:14,800 --> 00:07:15,250 quotes. 96 00:07:18,000 --> 00:07:21,270 Then also, it will have the same output as I've you. 97 00:07:22,990 --> 00:07:28,980 OK, next, we are going for the other program part here. 98 00:07:29,050 --> 00:07:30,640 One thing has to be remembered here. 99 00:07:30,650 --> 00:07:34,840 This one is that it was a different function because we are writing the function body. 100 00:07:35,320 --> 00:07:39,070 This Halbertal is that it was a different function because we are writing this function body. 101 00:07:39,550 --> 00:07:45,910 But this print is a function or the library function because we're not writing the function body of 102 00:07:45,910 --> 00:07:50,580 print, but we're using it, we're calling it according to our purpose. 103 00:07:50,950 --> 00:07:55,060 So that's why this print is known as the library function here. 104 00:07:57,580 --> 00:08:03,010 Here to define multiple functions, so first function is my addition, which takes X and Y as input 105 00:08:03,010 --> 00:08:03,490 document. 106 00:08:03,940 --> 00:08:10,840 So here the key word def there is a defined function name and then input arguments included within opening 107 00:08:11,440 --> 00:08:13,800 and closing must be there. 108 00:08:14,200 --> 00:08:20,710 And it is the body of the function which is printing this statement that is performing the addition, 109 00:08:20,710 --> 00:08:28,090 Operation and Return X, plus similarly written the function body for subtraction, performing the subtraction 110 00:08:28,090 --> 00:08:34,810 operation return X minus Y, my multiplication which is taking X and Y as input parameters, performing 111 00:08:34,810 --> 00:08:41,910 the multiplication operation Return X started while my division it is taking this X and Y as input arguments. 112 00:08:42,160 --> 00:08:46,330 So print performing that division, Operation Return X by white. 113 00:08:46,720 --> 00:08:52,980 So in this way we have defined my additions, my subtraction, my multiplication, my division methods 114 00:08:53,410 --> 00:08:57,280 now are going to define another method that is known as my menu. 115 00:08:57,310 --> 00:09:01,010 All these methods that user defined, user defined functions. 116 00:09:01,300 --> 00:09:05,040 So under this my menu are having this amendment will be printed. 117 00:09:05,040 --> 00:09:06,940 Therefore options are going to get printed. 118 00:09:07,480 --> 00:09:10,210 Then it blocks for the for the option number. 119 00:09:10,210 --> 00:09:12,490 Then it will accept the option number he had. 120 00:09:12,490 --> 00:09:16,080 Input is a function library function which returns a string. 121 00:09:16,390 --> 00:09:22,330 So before assigning it to the variable, I'm doing the typecasting using another inbuilt function library 122 00:09:22,330 --> 00:09:22,750 function. 123 00:09:22,750 --> 00:09:23,720 That is a.. 124 00:09:24,220 --> 00:09:30,580 So now it will return the value of S.H. So my menu returns the value of S.H. Let us go for the final 125 00:09:30,580 --> 00:09:35,440 function that is about UNDEF calculation so that it is calling my menu. 126 00:09:35,710 --> 00:09:41,920 So my menu will print this, my menu and the four options and it will accept for the choice and then 127 00:09:41,920 --> 00:09:46,150 the value will be returned to value which will be returned, will be assigning, will be assigned to 128 00:09:46,150 --> 00:09:46,330 this. 129 00:09:46,390 --> 00:09:52,630 S.H. It does not mean that here the variable is C.H. So here also we are supposed to have the variables 130 00:09:52,990 --> 00:09:55,140 and you can also make it as choice. 131 00:09:56,020 --> 00:10:05,200 So that is not an issue because this choice is is local within this menu and this is local within this 132 00:10:05,200 --> 00:10:05,800 calculation. 133 00:10:05,800 --> 00:10:11,050 So they may be having the same name, maybe having the different name, but in both cases they will 134 00:10:11,050 --> 00:10:15,350 have their separate memories with allocation during execution of our code. 135 00:10:15,850 --> 00:10:18,870 So here we have we agreed to number one. 136 00:10:18,880 --> 00:10:20,530 And number two, it was in the same way. 137 00:10:20,530 --> 00:10:21,730 Please enter the first number. 138 00:10:21,730 --> 00:10:26,140 Please enter the second number, input function returns string. 139 00:10:26,380 --> 00:10:29,340 So it has been converted to integer using IMT here. 140 00:10:29,830 --> 00:10:37,360 So if the value will be returned by this, if it is equal to one, then the result is equal to you. 141 00:10:37,360 --> 00:10:39,340 See, I'm calling this my addition function. 142 00:10:39,640 --> 00:10:42,340 And number two, I'm passing two parameters. 143 00:10:42,550 --> 00:10:45,970 This number and number will get assigned onto X and Y. 144 00:10:45,970 --> 00:10:50,860 You can find it and then it to return the sum of it after printing this line. 145 00:10:51,610 --> 00:10:57,190 So that result will be assigned to this result so that some will be assigned to this result. 146 00:10:57,190 --> 00:11:03,340 Four tries to return the respective difference that will be assigned to result in case of choice three, 147 00:11:03,340 --> 00:11:08,590 the respective product will be returned and that will be assigned to the variable result in case of 148 00:11:08,590 --> 00:11:14,170 choice for the respective quotient will be returned and that will be assigned to the variable result 149 00:11:14,170 --> 00:11:14,430 here. 150 00:11:14,680 --> 00:11:17,350 So print result is able to result. 151 00:11:17,360 --> 00:11:18,570 So I'm going to print that one. 152 00:11:19,030 --> 00:11:22,020 So if I call this respective function, that is calculation. 153 00:11:22,690 --> 00:11:23,950 You're going to go for the calculation. 154 00:11:23,950 --> 00:11:27,850 You can find that it is at first it is calling this myman I. 155 00:11:27,850 --> 00:11:31,480 My menu is displaying the options and it is asking for the option number. 156 00:11:31,990 --> 00:11:35,560 If I give the option number one, then it is asking for the first number. 157 00:11:35,560 --> 00:11:39,040 Let it be one hundred and the second number, let it be two hundred. 158 00:11:39,280 --> 00:11:44,530 So as a result of that, as I selected option number one, so additions will be done between 100 and 159 00:11:44,530 --> 00:11:45,090 200. 160 00:11:45,400 --> 00:11:50,560 So as a result we are going to get 300 here if I execute the code once again. 161 00:11:51,190 --> 00:11:57,760 So here I'm going for the option number two, I'm going for the multiplication between 10 and 20 you 162 00:11:57,760 --> 00:11:58,570 can easily find. 163 00:11:58,870 --> 00:12:01,070 So I'm getting the I'm getting the subtraction. 164 00:12:01,300 --> 00:12:02,080 I've selected. 165 00:12:02,080 --> 00:12:02,650 Option number two. 166 00:12:02,650 --> 00:12:03,670 It is for the subtraction. 167 00:12:03,940 --> 00:12:06,570 So good getting here minus ten as the outcome. 168 00:12:06,970 --> 00:12:08,890 So let me show it once again. 169 00:12:09,370 --> 00:12:11,440 So here I'm going for option number two. 170 00:12:11,440 --> 00:12:12,580 That is a subtraction. 171 00:12:12,910 --> 00:12:16,420 So I'm just giving the value one hundred and giving the value forty. 172 00:12:16,690 --> 00:12:19,500 You can find that performing the subtraction operation. 173 00:12:19,870 --> 00:12:25,600 So because this particular line has got printed, because I wrote that one in our minds, a production 174 00:12:25,600 --> 00:12:32,110 method and then it is returning X Y and that will be assigned to the result and and later. 175 00:12:32,110 --> 00:12:34,300 So result is equal to the result. 176 00:12:34,300 --> 00:12:35,830 Variables value will get printed. 177 00:12:35,830 --> 00:12:41,260 So result is equal to sixty is getting printed because we have given inputs one hundred and forty. 178 00:12:41,260 --> 00:12:42,880 So the difference will be sixty here. 179 00:12:43,420 --> 00:12:45,160 So let us go for the third option. 180 00:12:45,430 --> 00:12:48,220 So option number three, so now it is multiplication. 181 00:12:48,520 --> 00:12:56,350 So if I give the value one hundred and twenty, so the result is coming as two thousand performing the 182 00:12:56,350 --> 00:12:57,070 multiplication of. 183 00:12:57,720 --> 00:13:00,000 So result is a goal to 2000 here. 184 00:13:00,420 --> 00:13:01,720 So let us go for the last one. 185 00:13:02,640 --> 00:13:06,650 So if I give the option number four, I'm supposed to perform the division operation. 186 00:13:06,690 --> 00:13:08,060 So the question will be returned. 187 00:13:08,460 --> 00:13:11,570 I'm giving this one hundred and twenty five. 188 00:13:11,610 --> 00:13:14,020 You can find that it is giving me the result. 189 00:13:14,040 --> 00:13:20,190 As for Bunjil, so in this way, you can easily find that we have written some code here for the better 190 00:13:20,190 --> 00:13:22,790 explanation and for your better understanding. 191 00:13:23,370 --> 00:13:28,740 So a function can return, a return value may not return a value. 192 00:13:28,950 --> 00:13:34,500 A function should should be starting with this difficult which is defined then the function name, then 193 00:13:34,500 --> 00:13:35,160 opening bracket. 194 00:13:35,170 --> 00:13:41,730 Closing bracket will include the input parameters, then colon is master and all the all the statements 195 00:13:41,730 --> 00:13:43,500 which will be written with, with intent. 196 00:13:43,620 --> 00:13:46,340 So they will be treated within the function body. 197 00:13:46,430 --> 00:13:51,920 You can find out how we are writing this code and how the function is getting called here. 198 00:13:52,440 --> 00:13:57,170 So I think the conception is getting clear to you that how this function can work. 199 00:13:57,420 --> 00:13:59,070 So I'm giving you one assignment. 200 00:13:59,070 --> 00:14:01,080 We know that division by zero is illegal. 201 00:14:01,500 --> 00:14:06,120 So that's why whenever you are performing this division operation before performing the division, obviously 202 00:14:06,120 --> 00:14:07,500 I should check the value of Y. 203 00:14:07,920 --> 00:14:12,690 If the value of Y is negligible, then obviously I should handle the situation carefully. 204 00:14:12,930 --> 00:14:19,470 So please modify this code and so that you can also check with the division by zero is illegal, is 205 00:14:19,470 --> 00:14:25,690 happening or not, and accordingly modified this code and write and write the program yourself. 206 00:14:26,190 --> 00:14:27,390 Thanks for watching this video.