Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 Earlier in the course we were looking for a mean or an average function that Python might have to allow 1
us to make the average of the items of the numbers of a list. 2
And we found out that Python has some more fundamental functions such as len to get the number of items 3
of a list and sum to make the sum of the numbers of the list but it doesn't have a mean function. 4
So we ended up doing this. 5
So we had this list we calculated the sum using the sum function calculated the length. 6
So that number of items using the lend function and then did the division between sum and len to get them 7
mean. 8
However the fact that Python doesn't have a mean function doesn't mean we cannot create our own mean 9
function. 10
So what we are we going to do now is we're going to create our own mean function. To create functions 11
in Python you use the def keywords and then pick a name for your function. 12
The name has to follow the same naming rules as with variables so it cannot start with the number, 13
it cannot start with a math operator. 14
So let's say you mean, then you use round parentheses and inside those and the first line ends with 15
colon and inside the parentheses optionally you can input some parameters of your function. 16
Parameters in other words is the input that your function will get. 17
And that will process that's input and that will produce an an output, the input 18
in our case it would be a list. 19
However here you have to give a name following 20
again the same rules as with variables. 21
Let's say mylist. 22
Okay, then you want to press enter there and you see that's automatically Visual Studio code, 23
so the editor indented the next line with four spaces. 24
One,, two, three, four. 25
So that's what the syntax requires that after the first line. 26
So after you have defined what name you want for your function and what parameters you want, you 27
have to indent the next two lines. 28
So either your editor will do it automatically, but if it doesn't do it one, two, three, four. 29
Just indents four spaces in there. 30
You can also make it with one space, so just one space would also work plus it's recommended to use 31
four spaces, everyone uses for spaces because it's more visible the indented code. 32
So it will make your code more readable and then you can start to calculate the mean, let's say the_mean. 33
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
list divided by the length of my list and next. 35
What we want to do we want to use the return keyword and that is followed by what you want to return. 36
Well we want to return the mean. 37
Now save that, and let me open a terminal I want to execute these scripts now. 38
So let me show you what will happen. 39
Nothing will actually happen. 40
Nothing happens because what Python does is it just reads this definition. 41
This is like a blueprint. You have declared what your functions should do. 42
But you didn't tell Python to execute your function. To execute your function 43
you want to call the function, 44
pass an input value for that parameter that you defined, and also you want to print out, print open parentheses 45
there, close parentheses there, save don't forget, and execute. 46
That's the mean of those three numbers, so like that I can easily try this out with many input values just 47
like that I don't have to rewrite anything in here. 48
That's the beauty of a function. 49
So you can see the similarity here. 50
Just like we use the sum function, we are using the mean function in the same way. 51
So sum gets this input value, mean gets to this input value. 52
This happens to be a variable. 53
This happens to be a direct value, but it doesn't matter. If you check the type of mean, type mean, and 54
type of sum what you're going to see is that mean is a function. 55
So that was printed out, this was printed out by this, so mean is a function and sum is again a function, 56
but it's a built in function so it's built in in the Python interpreter, It was designed by the python 57
core development team and this function was designed by me, the programmer. 58
So that is how you create and call a function in Python.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.