Earlier in the course, we were looking for a
mean or an average function that Python might have
to allow us to make the average of the
items of the numbers of a list.
And we found out that Python has some more fundamental functions,
such as len to get the number of items of a
list, and sum to make the sum of the numbers of
a list, but it doesn't a mean function.
So we ended up doing this.
So we had this list.
We calculated the sum using the sum function, calculated
the length so the number of items using the
len function, and then did the division between sum
and length to get the mean.
However, the fact that Python doesn't have a mean function
doesn't mean we cannot create our own mean function.
So what we're going to do now is
we're going to create our own mean function.
To create functions in Python, you use the def
keyword and then pick a name for your function.
The name has to follow the same naming rules
as with variables, so it cannot start with a
number, it cannot start with a math operator.
So let's say mean.
Then you use round parentheses and inside
those and the first line ends with
a colon and inside the parentheses,
optionally, you can input some
parameters of your function.
Parameters, in other words, is input that your
function will get and that will process that
input and that will produce an output.
The input in our case would be a list.
However, here you have to give a name
following again the same rules as with variables.
Let's say mylist, okay?
Then you want to press Enter there and
you see that automatically Visual Studio Code,
so the editor indented the next
line with four spaces, 1, 2, 3, 4.
So that's what the syntax requires,
that after the first line,
so after you have defined what name you
want for your function and what parameters you
want, you have to indent the next lines.
So either your editor will do it
automatically, but if it doesn't do it
1, 2, 3, 4, just indent four spaces in there.
You can also make it with one space.
So just one space would also work,
but it's recommended to use four spaces.
Everyone uses four spaces because it's
more visible, the indented code.
So it will make your code more readable.
And then you can start to calculate the mean.
Let's say the_mean.
I'm going to call it like that so
I don't confuse it with the function name.
That would be the sum of mylist
divided by the length of mylist.
And next what we want to do, we
want to use the return keyword and that
is followed by what you want to return.
Well, we want to return the mean.
Now save that and let me open a terminal.
I want to execute this script now.
So let me show you what will happen.
Nothing will actually happen, nothing happens,
because what Python does is
it just reads this definition.
This is like a blueprint.
You have declared what your function should do, but
you didn't tell Python to execute your function.
To execute your function, you want to
call the function,
[Author typing]
pass an input value
for that parameter that you defined.
And also you want to print out,
print open parentheses there, close parentheses there,
save, don't forget and execute.
And that's the mean of those three numbers.
[No audio]
So like that, I can easily try
this out with many input values. Just like that.
I don't have to rewrite anything in here.
That's the beauty of a function.
So you can see the similarity here.
Just like we use the sum function, we are
using the mean function in the same way.
So sum gets this input value,
mean gets this input value.
This happens to be a variable.
This happens to be a direct value,
but it doesn't matter. If you check the type of
mean, type mean and type of sum, what you're going
to see is that mean is a function.
So that was printed out.
This was printed out by this.
So mean is a function and sum is again,
it's a function, but it's a built in function.
So it's built in in the Python interpreter.
It was designed by the Python core development team
and this function was designed by me, the programmer.
So that is how you create
and call a function in Python.
[Outro sound]