Before we begin working with the standard modules and libraries within Python,
I want us to create our very own custom module
and then use that module in one of our programs. OK? So
this is our main program file in here. The main dot pi
what I'm going to do is I'm going to create a new file.
OK?
And then call this one math
underscore A
dot py.
OK?
This is gonna be the file that will hold our very own custom module.
Now the module we're gonna design will be one that's very
simple is gonna take two numbers and then add them together.
OK?
So remember how we created our custom functions. We use the THEIN function
and now I'm going to add the name of
the function which will be add underscore numbers and then
two variables is gonna take two numbers and add them.
We can use any letters to represent them. I'm gonna go with F and V
just as an example and then add my column at the end.
And now what do I want the function to do? I want to add F and V. So I'm gonna say return
the value of watts of F plus V.
And there it is,
we now have our custom module
that will add two variables F and V and then return the result. OK.
If I was to go back to my main
P
file now,
and I want to make use of this module that we've designed in the method add pi file.
What I'm gonna do right now is I will have to use the function called imports.
This is the function we now need to use to
import
our cluster module. So
it's going to be the name of the file which is math underscore add.
OK?
And now
what do we need to do?
We need to provide two numbers.
OK?
So
I can say number one equals five and then number two equals seven.
OK?
Just as an example.
All right,
now we want to print out the results
of adding number one and number two.
So oh by the way, do forgive me, there shouldn't be any space between number one
and number two to forgive me
since it's a variable
so that no spaces in the verbal names to forgive me.
So we want to print out this result so
I can assign the variable called result to be equal to what
I am now going to pull in
the custom module which was math
on the score add.
OK.
And now dot
What was the function,
the function that we had
in our method add file is add underscore numbers.
Ok. So now I'm gonna go back in here
to my main file and I'll say
add
underscore numbers.
And now in brackets, what are we adding?
We're adding number one
and
number two.
And there it is.
All I need to do right now is to simply print out the results.
I'll see the print and then in brackets,
let's add a code and then I can say
the sum is and
I can add
comma and then simply say results
and there it is.
And now if I run the program,
there you go. It says the sum is 12.
So to give you a quick recap, first of all, we created a separate file
and notice by the way that the files are in the same folder. OK?
That's very, very important. So
we created a file called math underscore add.
And this was gonna be our custom module.
We defined the name of the function that we add underscore numbers,
it takes in any two variables F and V
and then it will return the value of adding those two variables.
So going back to our main file. Now, the first thing we needed to do
was to import that custom module, which is math underscore add in here,
we now need to provide the values of our two variables.
I don't want you to get confused. Don't think that.
Oh Because in our custom module, we used F and V
therefore
in the main file
F should be equal to five and then we should be equal to seven.
Why are we using number one? Number two?
Always keep in mind that these variables,
these parameters in here they represent basically nothing
is just a way to tell the functional or tell Python that hey,
within this function,
we're going to accept two variables and then we're going to add them together.
We could have used any letters in here. We could have used BC
Z.
We could have used X, we could have used anything, we could have used number one,
number two, we could use
any name, anything to represent those parameters. OK?
So don't think that whatever parameters you specify in here must be the same in here.
No. OK.
All Python should know is that
this function is going to take in two variables,
is going to take in two numbers and then add them.
OK?
So in here we said number one equals five, number two equals seven.
And now this is the tricky part. OK. Line six,
we want to print out the results. So I created a verb called result
which would not be equal to first of all the name of the module, which is math dot add
and then dot
Because we want to append the function that we created the function here is add ons
numbers.
So we appended it to math underscore add
and now in brackets simply number one number two.
And then the final thing is just print out the results. So we said the sum is
and then the result and that's how we got.
So, congratulations.
You've written your very first custom module and you've successfully been
able to import and make use of it as well.
Thank you for watching the video. I'll see you in the next class.