Hi again, in this video you'll learn how to create a function in Python and how
to use that function, so let me go ahead and create a script. Let me name this my
function.py and let's go ahead and create a function. Creating a function
will always begin with the DEF keyword which is, which means define. Now that is
followed by the name of the function, so you need to give your function a name
let me name this minutes to hours and as you may guess already what I'm
creating is a function that will convert minutes to hours, so you create the
procedure on how to convert minutes to hours and then you can call that
procedure, so you can call that function. You can give that function any values
when you call it and the function will produce an output depending on the value
and depending on the formula, on the procedures that you write in here, so
let's go ahead and do that, minutes to hours. Now functions normally they have
some input and they produce some output. The input goes inside these brackets
here. In our case the input could be minutes so this is just a variable, you
can give it any name that you want, you can give it any name that you want for
the function name as well, so the input is minutes and then the output will be
hours, but you don't write the output here. Here what to do you always choose a
column, press enter and that will be auto indented, so as you see the functions
everything that you write below the first line of a function definition is
indented and every line after that, so the DEF keyword, the name of function, the
input and here we write the body of the function. What we want to do is let's
say variable house equals to minutes / 60 that should give us the
hours. Note that if you are on Python 2 you want to do 60 point zero because
if you do that you get an integer as an output, not a float, so in
Python 2 you have to explicitly pass a float when you do division to get a
number, to get a float with decimal point. I am in Python 3, so I don't
need to put 60.0 there. And yeah, this is a simple function, so this is what the
function will perform and then the last line of the function is normally a
return keyword, so notice that these are reserved keywords def and return, and
what I want to return is, well gets what? The output which is hours, so that's the
idea, we are getting minutes. The user will input a value for that later when
you call a function and Then minutes will be divided by 60 and that operation,
that number will be stored in the hours variable which is a variable that exists
only inside the function so this is called a local variable. If you call it
outside the function it will not work, but we'll cover that later. And so then
we return the value of that variable. Now I'll save this and open the terminal.
Make some space there and let me execute the script. You get nothing and the
reason for that is because what you did here is you just define a blueprint, you
are not using the blueprint, so what you do next is you need to go ahead and use
that blueprint, so which means you need to call that function, so backspace
to go here to unindent. Indenting means that you are done with the
function definition so when you start a new line,
right here that means you are not writing inside function and so to call a
function what you do is you just call the name of the function, minutes to hours
and then you open brackets, then inside brackets you pass a value that you want
to use as input, so let's say you want 70 minutes, you want to convert them to
hours, save the script go and execute again you don't get any output, but let's see.
As I said you won't get any output, so why is that?
Well that's because you didn't perform a print operation there, you didn't call a
print function so what you do is you print so open brackets close brackets
and inside brackets give the function code, save your script, go here, execute
and yeah you get the hours, so the minutes is equal to one point one six
six six hours so it's important to know that this value here, this is just a
number, so you can see that by doing type. Execute. Sorry, I didn't print it out.
And so this is a float number, so just a number. The function gets an input and it
generates an output which is defined by the hours variable so whatever is
storing the hours variable, that is the value that the function will output and
yeah, you can do things like you know you can use this output to do other
operations, let's say you want to add ten hours there and you want to print that
output, so expect this to be 11.1666. And so on, and of course
now in your script you can add more functions if you like, you know like that.
Like seconds to hours. You pass seconds there
and here seconds. Return hours and so on and of course this
works separately, so you can say print seconds to hours and past there actually
we have to change this because this would be divided by 3600, and let's say
this number of seconds, execute and you get 2.0 hours and you also get the other
output in here and this one here. Now something important to know is that
these are custom functions and this one here is also function but this is a
built in function, so constant functions are functions created by you and built in
functions are functions that are created by someone else, so in this case print is
created by Python authors and print also has its own code, but it's more a
low-level code and it's actually read-only, so you won't be able to see
the code for print. Yeah, this is an introduction to functions. In the next
lecture I'll show you some more advanced features of functions, so let's see
you there.