1 00:00:00,009 --> 00:00:05,269 Before we begin working with the standard modules and libraries within Python, 2 00:00:05,469 --> 00:00:10,279 I want us to create our very own custom module 3 00:00:10,600 --> 00:00:14,239 and then use that module in one of our programs. OK? So 4 00:00:14,520 --> 00:00:17,819 this is our main program file in here. The main dot pi 5 00:00:18,110 --> 00:00:20,909 what I'm going to do is I'm going to create a new file. 6 00:00:21,069 --> 00:00:21,750 OK? 7 00:00:22,079 --> 00:00:25,510 And then call this one math 8 00:00:26,100 --> 00:00:27,059 underscore A 9 00:00:27,979 --> 00:00:29,270 dot py. 10 00:00:29,639 --> 00:00:30,200 OK? 11 00:00:30,739 --> 00:00:35,380 This is gonna be the file that will hold our very own custom module. 12 00:00:35,689 --> 00:00:38,259 Now the module we're gonna design will be one that's very 13 00:00:38,270 --> 00:00:42,060 simple is gonna take two numbers and then add them together. 14 00:00:42,369 --> 00:00:43,020 OK? 15 00:00:43,209 --> 00:00:48,459 So remember how we created our custom functions. We use the THEIN function 16 00:00:49,099 --> 00:00:50,900 and now I'm going to add the name of 17 00:00:50,909 --> 00:00:53,799 the function which will be add underscore numbers and then 18 00:00:54,119 --> 00:00:56,720 two variables is gonna take two numbers and add them. 19 00:00:56,860 --> 00:01:01,479 We can use any letters to represent them. I'm gonna go with F and V 20 00:01:01,729 --> 00:01:04,440 just as an example and then add my column at the end. 21 00:01:05,110 --> 00:01:12,129 And now what do I want the function to do? I want to add F and V. So I'm gonna say return 22 00:01:12,459 --> 00:01:16,839 the value of watts of F plus V. 23 00:01:17,750 --> 00:01:18,699 And there it is, 24 00:01:19,360 --> 00:01:22,730 we now have our custom module 25 00:01:23,129 --> 00:01:28,980 that will add two variables F and V and then return the result. OK. 26 00:01:30,309 --> 00:01:32,559 If I was to go back to my main 27 00:01:32,900 --> 00:01:33,199 P 28 00:01:33,489 --> 00:01:34,760 file now, 29 00:01:35,099 --> 00:01:41,000 and I want to make use of this module that we've designed in the method add pi file. 30 00:01:41,230 --> 00:01:46,760 What I'm gonna do right now is I will have to use the function called imports. 31 00:01:47,519 --> 00:01:50,220 This is the function we now need to use to 32 00:01:50,379 --> 00:01:51,760 import 33 00:01:52,099 --> 00:01:54,019 our cluster module. So 34 00:01:54,139 --> 00:01:58,930 it's going to be the name of the file which is math underscore add. 35 00:01:59,760 --> 00:02:00,410 OK? 36 00:02:01,199 --> 00:02:02,040 And now 37 00:02:02,379 --> 00:02:03,819 what do we need to do? 38 00:02:04,099 --> 00:02:07,589 We need to provide two numbers. 39 00:02:07,800 --> 00:02:08,508 OK? 40 00:02:08,710 --> 00:02:09,350 So 41 00:02:09,660 --> 00:02:18,779 I can say number one equals five and then number two equals seven. 42 00:02:19,070 --> 00:02:19,699 OK? 43 00:02:19,910 --> 00:02:21,220 Just as an example. 44 00:02:21,479 --> 00:02:21,979 All right, 45 00:02:23,350 --> 00:02:26,800 now we want to print out the results 46 00:02:27,139 --> 00:02:31,169 of adding number one and number two. 47 00:02:31,389 --> 00:02:36,000 So oh by the way, do forgive me, there shouldn't be any space between number one 48 00:02:36,460 --> 00:02:38,259 and number two to forgive me 49 00:02:38,360 --> 00:02:39,690 since it's a variable 50 00:02:39,809 --> 00:02:42,339 so that no spaces in the verbal names to forgive me. 51 00:02:42,429 --> 00:02:45,029 So we want to print out this result so 52 00:02:45,169 --> 00:02:48,990 I can assign the variable called result to be equal to what 53 00:02:49,389 --> 00:02:51,289 I am now going to pull in 54 00:02:51,899 --> 00:02:53,839 the custom module which was math 55 00:02:54,130 --> 00:02:56,199 on the score add. 56 00:02:57,199 --> 00:02:57,929 OK. 57 00:02:58,339 --> 00:02:59,979 And now dot 58 00:03:00,490 --> 00:03:01,899 What was the function, 59 00:03:02,100 --> 00:03:03,710 the function that we had 60 00:03:04,050 --> 00:03:09,539 in our method add file is add underscore numbers. 61 00:03:09,800 --> 00:03:11,860 Ok. So now I'm gonna go back in here 62 00:03:12,660 --> 00:03:14,240 to my main file and I'll say 63 00:03:15,410 --> 00:03:16,190 add 64 00:03:17,149 --> 00:03:19,479 underscore numbers. 65 00:03:19,610 --> 00:03:22,000 And now in brackets, what are we adding? 66 00:03:22,199 --> 00:03:23,690 We're adding number one 67 00:03:24,410 --> 00:03:25,220 and 68 00:03:25,550 --> 00:03:27,210 number two. 69 00:03:28,240 --> 00:03:29,139 And there it is. 70 00:03:29,929 --> 00:03:32,669 All I need to do right now is to simply print out the results. 71 00:03:32,679 --> 00:03:34,710 I'll see the print and then in brackets, 72 00:03:35,089 --> 00:03:37,149 let's add a code and then I can say 73 00:03:37,490 --> 00:03:40,309 the sum is and 74 00:03:41,250 --> 00:03:42,690 I can add 75 00:03:43,710 --> 00:03:45,960 comma and then simply say results 76 00:03:46,649 --> 00:03:47,470 and there it is. 77 00:03:48,419 --> 00:03:50,119 And now if I run the program, 78 00:03:50,440 --> 00:03:53,320 there you go. It says the sum is 12. 79 00:03:54,270 --> 00:03:59,389 So to give you a quick recap, first of all, we created a separate file 80 00:03:59,619 --> 00:04:02,800 and notice by the way that the files are in the same folder. OK? 81 00:04:02,809 --> 00:04:04,009 That's very, very important. So 82 00:04:04,410 --> 00:04:07,169 we created a file called math underscore add. 83 00:04:07,550 --> 00:04:09,729 And this was gonna be our custom module. 84 00:04:09,740 --> 00:04:12,539 We defined the name of the function that we add underscore numbers, 85 00:04:12,550 --> 00:04:15,330 it takes in any two variables F and V 86 00:04:15,479 --> 00:04:19,000 and then it will return the value of adding those two variables. 87 00:04:19,140 --> 00:04:23,380 So going back to our main file. Now, the first thing we needed to do 88 00:04:23,570 --> 00:04:28,049 was to import that custom module, which is math underscore add in here, 89 00:04:28,369 --> 00:04:32,410 we now need to provide the values of our two variables. 90 00:04:32,470 --> 00:04:35,519 I don't want you to get confused. Don't think that. 91 00:04:35,529 --> 00:04:39,390 Oh Because in our custom module, we used F and V 92 00:04:39,510 --> 00:04:40,510 therefore 93 00:04:40,739 --> 00:04:41,940 in the main file 94 00:04:42,100 --> 00:04:44,670 F should be equal to five and then we should be equal to seven. 95 00:04:44,679 --> 00:04:46,410 Why are we using number one? Number two? 96 00:04:46,739 --> 00:04:48,880 Always keep in mind that these variables, 97 00:04:48,890 --> 00:04:51,670 these parameters in here they represent basically nothing 98 00:04:51,959 --> 00:04:55,309 is just a way to tell the functional or tell Python that hey, 99 00:04:55,920 --> 00:04:57,100 within this function, 100 00:04:57,149 --> 00:05:01,420 we're going to accept two variables and then we're going to add them together. 101 00:05:01,480 --> 00:05:06,329 We could have used any letters in here. We could have used BC 102 00:05:06,700 --> 00:05:07,579 Z. 103 00:05:07,589 --> 00:05:10,899 We could have used X, we could have used anything, we could have used number one, 104 00:05:10,910 --> 00:05:11,899 number two, we could use 105 00:05:12,190 --> 00:05:14,380 any name, anything to represent those parameters. OK? 106 00:05:14,390 --> 00:05:19,290 So don't think that whatever parameters you specify in here must be the same in here. 107 00:05:19,299 --> 00:05:20,269 No. OK. 108 00:05:20,779 --> 00:05:22,829 All Python should know is that 109 00:05:23,390 --> 00:05:25,790 this function is going to take in two variables, 110 00:05:25,799 --> 00:05:27,980 is going to take in two numbers and then add them. 111 00:05:27,989 --> 00:05:28,399 OK? 112 00:05:28,579 --> 00:05:31,329 So in here we said number one equals five, number two equals seven. 113 00:05:31,579 --> 00:05:34,769 And now this is the tricky part. OK. Line six, 114 00:05:35,200 --> 00:05:39,350 we want to print out the results. So I created a verb called result 115 00:05:39,459 --> 00:05:45,010 which would not be equal to first of all the name of the module, which is math dot add 116 00:05:45,320 --> 00:05:46,670 and then dot 117 00:05:46,839 --> 00:05:51,279 Because we want to append the function that we created the function here is add ons 118 00:05:51,500 --> 00:05:52,230 numbers. 119 00:05:52,450 --> 00:05:55,190 So we appended it to math underscore add 120 00:05:55,350 --> 00:05:58,179 and now in brackets simply number one number two. 121 00:05:58,480 --> 00:06:01,869 And then the final thing is just print out the results. So we said the sum is 122 00:06:01,980 --> 00:06:04,309 and then the result and that's how we got. 123 00:06:04,420 --> 00:06:05,619 So, congratulations. 124 00:06:05,839 --> 00:06:08,929 You've written your very first custom module and you've successfully been 125 00:06:08,940 --> 00:06:11,549 able to import and make use of it as well. 126 00:06:11,600 --> 00:06:14,420 Thank you for watching the video. I'll see you in the next class.