Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,330 --> 00:00:07,620
Earlier in the course we were looking for a mean or an average function that Python might have to allow
1
2
00:00:07,620 --> 00:00:12,970
us to make the average of the items of the numbers of a list.
2
3
00:00:13,020 --> 00:00:19,980
And we found out that Python has some more fundamental functions such as len to get the number of items
3
4
00:00:19,980 --> 00:00:26,520
of a list and sum to make the sum of the numbers of the list but it doesn't have a mean function.
4
5
00:00:26,550 --> 00:00:28,410
So we ended up doing this.
5
6
00:00:28,410 --> 00:00:33,330
So we had this list we calculated the sum using the sum function calculated the length.
6
7
00:00:33,330 --> 00:00:39,660
So that number of items using the lend function and then did the division between sum and len to get them
7
8
00:00:39,660 --> 00:00:40,560
mean.
8
9
00:00:40,560 --> 00:00:48,750
However the fact that Python doesn't have a mean function doesn't mean we cannot create our own mean
9
10
00:00:48,750 --> 00:00:49,230
function.
10
11
00:00:49,470 --> 00:00:56,130
So what we are we going to do now is we're going to create our own mean function. To create functions
11
12
00:00:56,130 --> 00:01:01,050
in Python you use the def keywords and then pick a name for your function.
12
13
00:01:01,150 --> 00:01:07,560
The name has to follow the same naming rules as with variables so it cannot start with the number,
13
14
00:01:07,560 --> 00:01:09,960
it cannot start with a math operator.
14
15
00:01:09,960 --> 00:01:17,970
So let's say you mean, then you use round parentheses and inside those and the first line ends with
15
16
00:01:18,090 --> 00:01:26,310
colon and inside the parentheses optionally you can input some parameters of your function.
16
17
00:01:26,310 --> 00:01:29,870
Parameters in other words is the input that your function will get.
17
18
00:01:30,090 --> 00:01:34,140
And that will process that's input and that will produce an an output, the input
18
19
00:01:34,140 --> 00:01:36,800
in our case it would be a list.
19
20
00:01:36,870 --> 00:01:39,330
However here you have to give a name following
20
21
00:01:39,330 --> 00:01:41,820
again the same rules as with variables.
21
22
00:01:42,190 --> 00:01:44,440
Let's say mylist.
22
23
00:01:44,530 --> 00:01:52,170
Okay, then you want to press enter there and you see that's automatically Visual Studio code,
23
24
00:01:52,260 --> 00:01:56,410
so the editor indented the next line with four spaces.
24
25
00:01:56,580 --> 00:01:58,410
One,, two, three, four.
25
26
00:01:59,520 --> 00:02:02,970
So that's what the syntax requires that after the first line.
26
27
00:02:03,300 --> 00:02:09,390
So after you have defined what name you want for your function and what parameters you want, you
27
28
00:02:09,390 --> 00:02:12,560
have to indent the next two lines.
28
29
00:02:12,810 --> 00:02:18,930
So either your editor will do it automatically,
but if it doesn't do it one, two, three, four.
29
30
00:02:19,170 --> 00:02:21,300
Just indents four spaces in there.
30
31
00:02:21,360 --> 00:02:29,610
You can also make it with one space, so just one space would also work plus it's recommended to use
31
32
00:02:29,610 --> 00:02:35,240
four spaces, everyone uses for spaces because it's more visible the indented code.
32
33
00:02:35,370 --> 00:02:42,810
So it will make your code more readable and then you can start to calculate the mean, let's say the_mean.
33
34
00:02:43,290 --> 00:02:50,940
I'm going to call it like that so I don't confuse it with the function name, that would be the sum of my
34
35
00:02:51,660 --> 00:03:00,420
list divided by the length of my list and next.
35
36
00:03:00,420 --> 00:03:07,740
What we want to do we want to use the return keyword
and that is followed by what you want to return.
36
37
00:03:08,040 --> 00:03:09,700
Well we want to return the mean.
37
38
00:03:09,740 --> 00:03:17,580
Now save that, and let me open a terminal I want to execute these scripts now.
38
39
00:03:17,580 --> 00:03:20,460
So let me show you what will happen.
39
40
00:03:20,610 --> 00:03:22,860
Nothing will actually happen.
40
41
00:03:23,250 --> 00:03:28,470
Nothing happens because what Python does is it just reads this definition.
41
42
00:03:28,470 --> 00:03:33,270
This is like a blueprint. You have declared what your functions should do.
42
43
00:03:33,270 --> 00:03:38,550
But you didn't tell Python to execute your function.
To execute your function
43
44
00:03:38,550 --> 00:03:40,920
you want to call the function,
44
45
00:03:43,640 --> 00:03:54,980
pass an input value for that parameter that you defined, and also you want to print out, print open parentheses
45
46
00:03:54,980 --> 00:04:01,670
there, close parentheses there, save don't forget, and execute.
46
47
00:04:01,700 --> 00:04:12,960
That's the mean of those three numbers, so like that
I can easily try this out with many input values just
47
48
00:04:12,960 --> 00:04:17,310
like that I don't have to rewrite anything in here.
48
49
00:04:17,380 --> 00:04:19,130
That's the beauty of a function.
49
50
00:04:19,200 --> 00:04:22,540
So you can see the similarity here.
50
51
00:04:22,770 --> 00:04:29,600
Just like we use the sum function, we are using the mean function in the same way.
51
52
00:04:30,120 --> 00:04:35,680
So sum gets this input value, mean gets to this input value.
52
53
00:04:35,700 --> 00:04:36,890
This happens to be a variable.
53
54
00:04:36,890 --> 00:04:45,390
This happens to be a direct value, but it doesn't matter.
If you check the type of mean, type mean, and
54
55
00:04:45,390 --> 00:04:53,920
type of sum what you're going to see is that mean is a function.
55
56
00:04:54,000 --> 00:05:04,860
So that was printed out, this was printed out by this,
so mean is a function and sum is again a function,
56
57
00:05:04,890 --> 00:05:10,980
but it's a built in function so it's built in in the Python interpreter, It was designed by the python
57
58
00:05:11,490 --> 00:05:16,940
core development team and this function was designed by me, the programmer.
58
59
00:05:17,130 --> 00:05:21,920
So that is how you create and call a function in Python.
6534
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.