In this python example, I will create a simple python function to calculate the compound interest which will then be used to calculate the future sum of money (F) if the present sum of money (P) and the interest rate per interest period are given.

Before I start to write the program, let us understand how are the future sum and the present sum of money relates to each other first.

Let’s say you have deposited $100 into a bank with a 6 percent yearly interest rate, then after a year you will have $100 + (0.06) * 100 in your bank account, which is F = P(1+i) where F is the future sum of money, P is the present sum of money and i is the interest rate per annum. If you keep the money in the bank then at the end of the second year you will get F = P(1+i)(1+i)! There is actually a formula that is used to find the future sum of money and the formula is F = P(F/P, i,n) where i is the interest rate per interest period, and n is the number of years, months, etc.

Now let us write the python program which can be used to calculate the future sum of money as follows:

def calculateFutureSum(p, i, n): f = p * pow((1+i),n) print(f)

Now let us try to enter the below parameters into the function, present sum of money p is 600, the interest rate per interest period is 3% or 0.03 and the interest period is 3 as well.

calculateFutureSum(600, 0.03, 3)

Then the program will generate the below answer.

655.6362

This means if you deposited 600 dollars into a bank now with a 3% interest rate per annum you will get 655.64 dollars at the end of the third year which is not that interesting as compared to use those money to invest in something else such as lending to your friend with 3% quarterly interest rate.

How about if you know the future sum of money and you want to find the present sum of money, for example, how much money that you need to put inside a bank if you expected $500 in your bank at the end of a year with 6% interest rate per annum? Then it is simply using this formula P = 500/(1+0.06) or simply P = F(P/F, i, n). Instead of create another function to calculate the present sum of money, let us create a unified function to calculate both of them.

def calculateFutureSum(amount, i, n, present): if present: f = amount * pow((1+i),n) print(f) else: p = amount / pow((1+i),n) print(p)

With this updated version of the function, you can find both the present value and the future value by just specifying whether the present boolean is true or false, if the present sum of money is unknown then the present boolean value is false and the amount simply means the future sum of money and vice versa!

Let’s find the present sum of money you need to put into your bank account if you expected $500 inside your bank at the end of first year with 6 percent annum bank interest.

calculateFutureSum(500, 0.06, 1, False)

The answer is $471.698!

Actually, this is a small part of the latest project I am going to create with the help of wxPython or Tkinter thus stay tuned for this latest project of mine!