1 00:00:00,000 --> 00:00:03,412 Earlier in the course, we were looking for a 2 00:00:03,413 --> 00:00:07,044 mean or an average function that Python might have 3 00:00:07,045 --> 00:00:09,492 to allow us to make the average of the 4 00:00:09,493 --> 00:00:12,772 items of the numbers of a list. 5 00:00:12,773 --> 00:00:16,858 And we found out that Python has some more fundamental functions, 6 00:00:16,859 --> 00:00:20,308 such as len to get the number of items of a 7 00:00:20,309 --> 00:00:23,748 list, and sum to make the sum of the numbers of 8 00:00:23,749 --> 00:00:26,428 a list, but it doesn't a mean function. 9 00:00:26,429 --> 00:00:28,396 So we ended up doing this. 10 00:00:28,397 --> 00:00:29,500 So we had this list. 11 00:00:29,501 --> 00:00:32,625 We calculated the sum using the sum function, calculated 12 00:00:32,626 --> 00:00:35,308 the length so the number of items using the 13 00:00:35,309 --> 00:00:38,578 len function, and then did the division between sum 14 00:00:38,579 --> 00:00:40,540 and length to get the mean. 15 00:00:40,541 --> 00:00:45,248 However, the fact that Python doesn't have a mean function 16 00:00:45,249 --> 00:00:49,328 doesn't mean we cannot create our own mean function. 17 00:00:49,329 --> 00:00:50,928 So what we're going to do now is 18 00:00:50,929 --> 00:00:55,010 we're going to create our own mean function. 19 00:00:55,011 --> 00:00:57,908 To create functions in Python, you use the def 20 00:00:57,909 --> 00:01:00,980 keyword and then pick a name for your function. 21 00:01:00,981 --> 00:01:04,238 The name has to follow the same naming rules 22 00:01:04,239 --> 00:01:06,968 as with variables, so it cannot start with a 23 00:01:06,969 --> 00:01:09,822 number, it cannot start with a math operator. 24 00:01:09,823 --> 00:01:11,528 So let's say mean. 25 00:01:11,529 --> 00:01:15,512 Then you use round parentheses and inside 26 00:01:15,513 --> 00:01:17,912 those and the first line ends with 27 00:01:17,913 --> 00:01:20,306 a colon and inside the parentheses, 28 00:01:20,307 --> 00:01:22,620 optionally, you can input some 29 00:01:22,621 --> 00:01:24,620 parameters of your function. 30 00:01:24,621 --> 00:01:28,476 Parameters, in other words, is input that your 31 00:01:28,477 --> 00:01:31,312 function will get and that will process that 32 00:01:31,313 --> 00:01:33,446 input and that will produce an output. 33 00:01:33,447 --> 00:01:36,670 The input in our case would be a list. 34 00:01:36,671 --> 00:01:38,768 However, here you have to give a name 35 00:01:38,769 --> 00:01:42,250 following again the same rules as with variables. 36 00:01:42,251 --> 00:01:45,076 Let's say mylist, okay? 37 00:01:45,077 --> 00:01:48,292 Then you want to press Enter there and 38 00:01:48,293 --> 00:01:52,132 you see that automatically Visual Studio Code, 39 00:01:52,133 --> 00:01:54,472 so the editor indented the next 40 00:01:54,473 --> 00:01:58,500 line with four spaces, 1, 2, 3, 4. 41 00:01:59,270 --> 00:02:01,486 So that's what the syntax requires, 42 00:02:01,487 --> 00:02:03,112 that after the first line, 43 00:02:03,113 --> 00:02:05,512 so after you have defined what name you 44 00:02:05,513 --> 00:02:08,412 want for your function and what parameters you 45 00:02:08,413 --> 00:02:12,674 want, you have to indent the next lines. 46 00:02:12,675 --> 00:02:14,348 So either your editor will do it 47 00:02:14,349 --> 00:02:16,866 automatically, but if it doesn't do it 48 00:02:16,867 --> 00:02:21,280 1, 2, 3, 4, just indent four spaces in there. 49 00:02:21,281 --> 00:02:23,376 You can also make it with one space. 50 00:02:23,377 --> 00:02:27,696 So just one space would also work, 51 00:02:27,697 --> 00:02:30,342 but it's recommended to use four spaces. 52 00:02:30,343 --> 00:02:32,362 Everyone uses four spaces because it's 53 00:02:32,363 --> 00:02:35,268 more visible, the indented code. 54 00:02:35,269 --> 00:02:37,434 So it will make your code more readable. 55 00:02:37,435 --> 00:02:40,612 And then you can start to calculate the mean. 56 00:02:40,613 --> 00:02:43,076 Let's say the_mean. 57 00:02:43,077 --> 00:02:44,872 I'm going to call it like that so 58 00:02:44,873 --> 00:02:47,928 I don't confuse it with the function name. 59 00:02:47,929 --> 00:02:52,340 That would be the sum of mylist 60 00:02:53,030 --> 00:02:57,880 divided by the length of mylist. 61 00:02:59,290 --> 00:03:01,612 And next what we want to do, we 62 00:03:01,613 --> 00:03:05,212 want to use the return keyword and that 63 00:03:05,213 --> 00:03:07,852 is followed by what you want to return. 64 00:03:07,853 --> 00:03:09,648 Well, we want to return the mean. 65 00:03:09,649 --> 00:03:14,070 Now save that and let me open a terminal. 66 00:03:14,071 --> 00:03:16,780 I want to execute this script now. 67 00:03:17,310 --> 00:03:19,580 So let me show you what will happen. 68 00:03:20,530 --> 00:03:24,100 Nothing will actually happen, nothing happens, 69 00:03:24,101 --> 00:03:25,956 because what Python does is 70 00:03:25,957 --> 00:03:28,362 it just reads this definition. 71 00:03:28,363 --> 00:03:30,202 This is like a blueprint. 72 00:03:30,203 --> 00:03:33,512 You have declared what your function should do, but 73 00:03:33,513 --> 00:03:37,272 you didn't tell Python to execute your function. 74 00:03:37,273 --> 00:03:39,640 To execute your function, you want to 75 00:03:39,641 --> 00:03:40,971 call the function, 76 00:03:40,972 --> 00:03:43,566 [Author typing] 77 00:03:43,567 --> 00:03:47,500 pass an input value 78 00:03:47,501 --> 00:03:49,830 for that parameter that you defined. 79 00:03:50,490 --> 00:03:52,940 And also you want to print out, 80 00:03:52,941 --> 00:03:57,710 print open parentheses there, close parentheses there, 81 00:03:57,711 --> 00:04:01,350 save, don't forget and execute. 82 00:04:01,351 --> 00:04:05,340 And that's the mean of those three numbers. 83 00:04:05,341 --> 00:04:07,466 [No audio] 84 00:04:07,470 --> 00:04:09,776 So like that, I can easily try 85 00:04:09,777 --> 00:04:13,332 this out with many input values. Just like that. 86 00:04:13,333 --> 00:04:17,170 I don't have to rewrite anything in here. 87 00:04:17,171 --> 00:04:19,428 That's the beauty of a function. 88 00:04:19,429 --> 00:04:22,724 So you can see the similarity here. 89 00:04:22,725 --> 00:04:26,376 Just like we use the sum function, we are 90 00:04:26,377 --> 00:04:29,944 using the mean function in the same way. 91 00:04:29,945 --> 00:04:32,500 So sum gets this input value, 92 00:04:33,270 --> 00:04:35,496 mean gets this input value. 93 00:04:35,497 --> 00:04:36,882 This happens to be a variable. 94 00:04:36,883 --> 00:04:39,036 This happens to be a direct value, 95 00:04:39,037 --> 00:04:42,556 but it doesn't matter. If you check the type of 96 00:04:42,557 --> 00:04:49,168 mean, type mean and type of sum, what you're going 97 00:04:49,169 --> 00:04:53,790 to see is that mean is a function. 98 00:04:53,791 --> 00:04:55,660 So that was printed out. 99 00:04:56,190 --> 00:04:58,992 This was printed out by this. 100 00:04:58,993 --> 00:05:04,116 So mean is a function and sum is again, 101 00:05:04,117 --> 00:05:06,308 it's a function, but it's a built in function. 102 00:05:06,309 --> 00:05:09,418 So it's built in in the Python interpreter. 103 00:05:09,419 --> 00:05:13,332 It was designed by the Python core development team 104 00:05:13,333 --> 00:05:17,090 and this function was designed by me, the programmer. 105 00:05:17,091 --> 00:05:19,732 So that is how you create 106 00:05:19,733 --> 00:05:21,770 and call a function in Python. 107 00:05:21,771 --> 00:05:25,333 [Outro sound]