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

2
00:00:07,620 --> 00:00:12,970
us to make the average of the items of the numbers of a list.

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

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.

5
00:00:26,550 --> 00:00:28,410
So we ended up doing this.

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.

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

8
00:00:39,660 --> 00:00:40,560
mean.

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

10
00:00:48,750 --> 00:00:49,230
function.

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

12
00:00:56,130 --> 00:01:01,050
in Python you use the def keywords and then pick a name for your function.

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,

14
00:01:07,560 --> 00:01:09,960
it cannot start with a math operator.

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

16
00:01:18,090 --> 00:01:26,310
colon and inside the parentheses optionally you can input some parameters of your function. 

17
00:01:26,310 --> 00:01:29,870
Parameters in other words is the input that your function will get.

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

19
00:01:34,140 --> 00:01:36,800
in our case it would be a list.

20
00:01:36,870 --> 00:01:39,330
However here you have to give a name following

21
00:01:39,330 --> 00:01:41,820
again the same rules as with variables.

22
00:01:42,190 --> 00:01:44,440
Let's say mylist.

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,

24
00:01:52,260 --> 00:01:56,410
so the editor indented the next line with four spaces.

25
00:01:56,580 --> 00:01:58,410
One,, two, three, four.

26
00:01:59,520 --> 00:02:02,970
So that's what the syntax requires that after the first line.

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

28
00:02:09,390 --> 00:02:12,560
have to indent the next two lines.

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.

30
00:02:19,170 --> 00:02:21,300
Just indents four spaces in there.

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

32
00:02:29,610 --> 00:02:35,240
four spaces, everyone uses for spaces because it's more visible the indented code.

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.

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

35
00:02:51,660 --> 00:03:00,420
list divided by the length of my list and next.

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.

37
00:03:08,040 --> 00:03:09,700
Well we want to return the mean.

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.

39
00:03:17,580 --> 00:03:20,460
So let me show you what will happen.

40
00:03:20,610 --> 00:03:22,860
Nothing will actually happen.

41
00:03:23,250 --> 00:03:28,470
Nothing happens because what Python does is it just reads this definition.

42
00:03:28,470 --> 00:03:33,270
This is like a blueprint. You have declared what your functions should do.

43
00:03:33,270 --> 00:03:38,550
But you didn't tell Python to execute your function.
To execute your function

44
00:03:38,550 --> 00:03:40,920
you want to call the function,

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

46
00:03:54,980 --> 00:04:01,670
there, close parentheses there, save don't forget, and execute.

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

48
00:04:12,960 --> 00:04:17,310
like that I don't have to rewrite anything in here.

49
00:04:17,380 --> 00:04:19,130
That's the beauty of a function.

50
00:04:19,200 --> 00:04:22,540
So you can see the similarity here.

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.

52
00:04:30,120 --> 00:04:35,680
So sum gets this input value, mean gets to this input value.

53
00:04:35,700 --> 00:04:36,890
This happens to be a variable.

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

55
00:04:45,390 --> 00:04:53,920
type of sum what you're going to see is that mean is a function.

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,

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

58
00:05:11,490 --> 00:05:16,940
core development team and this function was designed by me, the programmer.

59
00:05:17,130 --> 00:05:21,920
So that is how you create and call a function in Python.

