Python Beginner Projects - Compound Interest Calculator Function - Practice Programs Episode 1

LeMaster Tech
183 followers
37 Video Views·Apr 23, 2025
Prepare for a technical interview, practice building an app or get ready for an upcoming test or quiz with these sample projects! This episode covers creating a compound interest calculator inside of python and using user input data to output Advanced interest results!
This is episode 1 in a Python beginner project series coming from LeMaster Tech.
Be sure to give us a like, drop a comment, or subscribe to the channel for more content like this!
The previous video:
https://www.youtube.com/watch?v=n3oJHCgy9vE&t=1s
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
today's video is going to be episode 1
in a new series on beginner projects
with python and the first thing we're
going to do is take a look at
calculating compound interest in this
episode so
i have this image from the internet
of the
formula for compound interest and if
you're unfamiliar with compound interest
then you at least can just look at this
formula but essentially it's money that
you put into an account that accrues a
certain amount of interest over time
and what we're going to do is calculate
the final amount that you would have in
the bank so if you put 10 000 in the
bank leave it in there for a few years
depending on the interest rate you're
getting and the number of times it
compounds you're actually going to end
up with more money than you put in and
what we're going to do is create a
python tool for calculating that
so
i'm going to go ahead and start by
defining
a compound
interest function
and as we just saw in our formula i'm
gonna keep clicking back to this to get
an output we're going to need four
things principal
interest rate
number of times interest is applied per
time period and number of time periods
elapsed
so let's just go ahead and call them by
their variable names it was p
r n
and t
and again this is a useful tool even if
you don't really care about compound
interest it's a valuable
thing to kind of understand how to turn
a formula into a function within python
and if you're ever you know programming
a financial tool for an app or
any really any reason
it's a great it's a great skill to be
able to have to take a formula and turn
into a function
so let's go ahead and create a variable
called amount for our final amount
and we'll just
put the function in there so it was
p principle times
and then we're going to use this python
pow function so we can put everything
inside these parentheses
and then we can specify the power we
want to raise it to so it should be
1
plus
r divided by n
close parentheses close parenthesis
and then
leave oh it still needs to be inside
those
and then raised to the n t power
so this
function
at n times t
this function is
what we're saying is inside the power
function 1 plus r over n which is right
here
and then
comma and this is the power we're
raising it to and so that should do it
for that and then we're going to
go ahead and just separate out the
interest so we can make sure that our
um
that our compounding interest formula is
actually working
and that'll be amount minus p so this is
going to be final minus initial
and
that's so now we've gotten we have the
initial amount we have the amount of
interest and then we have the total so
really what's left is to display these
on screen so we'll go ahead and print
compound
interest
is
and we will
report interest here
and then we will print
initial
investment
was and print p here
and then we will print
total
will be
and let's print amount
and so this is pretty cool on its own
you can pass in a principal amount of
money we'll actually do one of these
here
um
we'll do compounding
interest if we passed in ten thousand
dollars
and we had a rate of five percent
although so this formula needs to have
it as a decimal
so rate of five percent we'll say it's
compounded once per year and then we'll
say we want to compound it for 10 years
and just running that
compound interest
oh i'm running main
compound
just running that we can see with an
initial investment of 10 grand
over 10 years you're getting 6 200
almost 6 300 of compound interest and it
tells you your total and this is pretty
cool on its own right here but we
actually can take this a bit farther and
make it a little more user friendly
because this is not necessarily how you
would want to enter this data
so let's go ahead and create basically a
function to
handle the prompts so we'll say compound
interest
prompts
and this function is not actually going
to need any inputs it's going to handle
all of it in turn
and we'll just start creating variables
for the different
we'll start creating variables inside
this function for the different uh
for the different variables we need in
our compound interest function
and so the principal amount will say we
want that to be an integer and then
we'll input and say
enter
initial
amount
and i'll do it
and then let's make another one and say
rate
and let's say that's going to be a float
because it could be decimal and input
enter
anticipated
interest rate
and we'll we'll ask for that as a
percentage and then we can actually
handle the math here
on our own
if we just divide by 100 right in here
so now once we get the actual rate
variable it's already coming through as
a decimal
and then we'll say n
input
is going to be now this one's a little
different and a little more specific to
compounding interest but
you want a compounding period so
something could
compound daily monthly or annually
depending on the fund and that's going
to affect what n ends up being which
will calculate that part next but we
need
we need them to specify we need the user
to specify how frequently it's
going to be getting compounded so let's
just ask for single letters
back
m for monthly
a for annually
and then you'll see we'll
actually handle
what to do with n input right now so if
n
input
is
a
then
the actual n variable which i'm going to
call frequency just because there's a
lot of ends in this program
frequency is actually going to be 1
that's going to be how many times per
year because we're going to say t is in
years technically this formula works
with any time frame but it's actually
just going to be easier if we do it in
years that's the most standard way
people calculate interest
so then if it's m it goes up to 12
because we're saying how many times it
compounds per year and then if it's
daily
we'll say
365
just because
um
because 364 doesn't catch leap years
and so now what we're doing is we're
telling we're having the user tell us
how frequently it compounds but then
they don't have to punch anything in for
n we do that math with the frequency
variable
and then the last input we're going to
want is let's make it another float and
say
input
enter
the
number of
years to grow
and that should do that and then the
last thing we have to do with this
formula is we're actually going to call
inside our compound interest prompts
formula we're going to call compound
interest but we're going to pass in the
variables that we created in this
function
so
frequency and time
so
just to explain what we did here we have
this function that actually handles
getting prompts from the users
and
putting it in
kind of conditions that the the compound
interest calculator can use
but this function is not doing any math
so then once we get all of our inputs we
can nest this other function inside of
it and pass these through
and you may be saying to yourself well
we already created this function why not
just build this
out inside the same function totally
valid you can do that this is to show if
you already have useful calculation
tools but you don't have a useful way of
gathering that info from a user this is
a way you could build an interface that
gets user input and then passes it
through your calculation tool
so then all we need to do outside of
that is just call our function
compound int prompts and when we run
this hopefully we'll see if i did
anything goofy
but here we go let's see ten thousand
uh let's do a rate of five and we can
enter it as a percent this time
let's say it compounds annually
and we're going to grow for 10 years so
this should be pretty close to what we
saw before yeah we get the exact same
six two eight eight point
nine four six
um and then the total together but this
is pretty cool i mean we can run it
again and let's do something
initial amount ten thousand but let's
say our interest rate is up to seven
percent
and we're going to compound daily
and we'll still grow for ten years so
you should see
with more frequent compounding and a
higher interest rate we should be a fair
bit higher
yeah there you go so we went from 6 200
to 10 000 and right away you see kind of
the
cool thing about compound interest so
let's say you're like a 30 year old
with a decent 401k savings and you have
50 grand in retirement and you have
another
you're expecting to average seven years
seven percent in the market fund
compounds daily and you've got another
35 years before you retire without
putting any more money in you're going
to accrue more than 500 grand just on 50
grand with just
sticking in a fund that gets percent per
year
so
this is a pretty cool calculator again
if you're more into programming than you
are into finance just understanding how
to take a function
and turn it into useful python code this
should be valuable for you
but there's a lot of scenarios where you
might be writing code for
a financial institution or you just need
to have a little bit of worldly
knowledge outside of the world of code
so
that's how you build a compound interest
calculator in python hopefully you found
it useful if you did it another way or
have any questions about what you saw in
here
feel free to drop me a comment and if
you found this or any of the content on
the channel
useful go ahead and
i'd really appreciate a like and
subscribe and
thanks for watching as always and good
luck with your code thanks bye
