This brief tutorial covers how to reference and import outside functions from .py python files in your local program directory! This is a super useful tool for creating large scale programs where subprograms and subfuctions and separated out into easy to read containers. We also cover how to alias those external files to shorten the name you need to access them.
If there's something else you want to see in a future video or a question you have on anything programming related, just let us know about it in the comments!
Thanks for watching, leave a like if you found it useful and subscribe to the channel for tons of new content coming every week!
hello welcome back to the channel and
today's video is going to be learning
how to import external programs from
external python files into your main
program in python
[Music]
this is a super useful concept because
in a lot of larger python programs
you're going to grab something from
stack overflow or github
or even just something you programmed in
the past and you don't want every
program and subroutine that you're going
to call in your function to be in the
main program so in this case i have two
functions uh two python files already
created in my directory
you don't need to copy these exact ones
down i just have two to show
how it works i have useless programs
which is just a function i called spam
random numbers that's going to print a
bunch of randomly generated integers
and then i have useful functions oops i
have useful functions which is going to
useful programs which is going to uh
either do compound interest or print hi
uh with the name you give it onto the
screen
what i have in these other files doesn't
really matter it's just so i have
something to reference from my main
but let's get started on the actual code
you need to write so if you're familiar
with like importing modules like if you
were going to use random gently
generated stuff you would do this import
random
if you were building a game like in pi
game you would import pygame well this
is pretty easy you just do import and
then the name of your file and as long
as it's a dot pi file and in the same
directory
um it can actually find that file just
by you start typing in the name so then
um we're going to just do import useless
programs useful programs just like that
and then i like to typically alias them
so uh like we just brought in useful
programs but i don't want every time i
call a program from that file i don't
want to have to type in the whole name
so i'll just call it good which you're
you're creating an alias variable
just saying this variable is equal to
this other thing but now anytime i want
to use it inside this program i can just
type good or bad instead of useless and
useful
so that's pretty handy
and this actually is all you need to do
to now be able to call functions from
those other programs so
one of the ones we had in the good
function was say hi
and actually something i want to
highlight here when you type in good dot
the the functions are going to show up
with an f it depends on what
development environment you're using but
most of them the functions in that
program file will show up here and so
you can see i have say hi but another
really useful thing is it will tell you
in the parentheses what variables it's
expecting and so hopefully a
well-written program the variable names
are descriptive enough that you can
actually
infer what you need to give it
so here it says name you can see we need
to give it a name and let's do that now
you can see in my function right here in
my
python file right here i haven't created
the say hi function i'm just calling
good which if we trace it back is in
useful programs and you can see all it's
going to do is print hi in whatever name
we give it onto the screen so let's go
ahead and run this
and we see it just as high pete onto the
screen so um right there that's the the
basics of it uh if you just came here
needing that exact um little piece then
you're probably good to go i'll just
flesh out a few other things here
on this video so the next thing i want
to do is
like the compound interest function
which actually has inputs and outputs in
it so you can see all you need to do to
run the compound interest calculation
is just call it because all the
variables are gathered as inputs inside
of this function and it's just going to
give you back a total so
in theory all we want to do is just
print out the compound interest function
like we just want to run it and then
it's going to give us the total so let's
go ahead and just take a look and see
let's say initial amount it doesn't
really matter 10 000 interest rate 10
20 years calculation
and it compounds daily
um okay so the reason i wanted to run
that is you can see
we called a function that gets inputs
and just returns outputs all externally
and that's pretty cool we got that right
there but this value obviously is not
useful for numerical uh values you can
still do things like
like rounding to your external functions
just like you could if they were written
here so you can use the round function
to say when we do return it i want just
two
decimal points
and so we'll run that again and while
i'm here i'll just throw in one from the
bad to show
we can do
we'll just do four random numbers as
well
so you can
do this for as many external python
files as you need so if you had one that
was a bunch of useful
math equations you'd set up one that was
a bunch of graphical things like drawing
buttons or whatever you would just have
like a library at the start of your
function
at the start of your program that's just
all of your import statements and then
if you decide to alias them all of your
alias statements so let's go ahead and
run that
and we're going to see the same initial
amount uh 7 interest rate
20 years and
it compounds monthly so 12.
okay so now you can see we got a rounded
var value for the compound interest
because we did that rounding here and
then uh we got four random numbers
because that's my useless program so
this was just intended to show kind of
how you would use functions that are
defined in external python files inside
of your
file hopefully you found it useful try
to keep this one short and simple
if there's anything else you want to see
or need help with just let me know about
in the comments below and i'll get back
to you as soon as i can
if there's anything you want to see in a
future video be sure to let me know
about that in the comments below
if you did find it useful be sure to
leave a like on the video subscribe to
the channel it helps me out a ton and as
always good luck with your code and
thanks for watching thanks bye