We can use the following compound interest formula to find the ending value of some investment after a certain amount of time:
A = P(1 + r/n)nt
where:
- A:Â Final Amount
- P:Â Initial Principal
- r:Â Annual Interest Rate
- n:Â Number of compounding periods per year
- t:Â Number of years
We can use the following formula to calculate the ending value of some investment in Python:
P*(pow((1+r/n), n*t))
And we can use the following function to display the ending value of some investment at the end of each period:
def each_year(P, r, n, t): for period in range(t): amount = P*(pow((1+r/n), n*(period+1))) print('Period:', period+1, amount) return amount
The following examples show how to use these formulas in Python to calculate the ending value of investments in different scenarios.
Example 1: Compound Interest Formula with Annual Compounding
Suppose we invest $5,000 into an investment that compounds at 6% annually.
The following code shows how to calculate the ending value of this investment after 10 years:
#define principal, interest rate, compounding periods per year, and total years P = 5000 r = .06 n = 1 t = 10 #calculate final amount P*(pow((1+r/n), n*t)) 8954.238482714272
This investment will be worth $8,954.24 after 10 years.
We can use the function we defined earlier to display the ending investment after each year during the 10-year period:
#display ending investment after each year during 10-year period
each_year(P, r, n, t)
Period: 1 5300.0
Period: 2 5618.000000000001
Period: 3 5955.08
Period: 4 6312.384800000002
Period: 5 6691.127888000002
Period: 6 7092.595561280002
Period: 7 7518.151294956803
Period: 8 7969.240372654212
Period: 9 8447.394795013464
Period: 10 8954.238482714272
This tells us:
- The ending value after year 1 was $5,300.
- The ending value after year 2 was $5,618.
- The ending value after year 3 was $5,955.08.
And so on.
Example 2: Compound Interest Formula with Monthly Compounding
Suppose we invest $1,000 into an investment that compounds at 6% annually and is compounded on a monthly basis (12 times per year).
The following code shows how to calculate the ending value of this investment after 5 years:
#define principal, interest rate, compounding periods per year, and total years P = 1000 r = .06 n = 12 t = 5 #calculate final amount P*(pow((1+r/n), n*t)) 1348.8501525493075
This investment will be worth $1,348.85 after 5 years.
Example 3: Compound Interest Formula with Daily Compounding
Suppose we invest $5,000 into an investment that compounds at 8% annually and is compounded on a daily basis (365 times per year).
The following code shows how to calculate the ending value of this investment after 15 years:
#define principal, interest rate, compounding periods per year, and total years P = 5000 r = .08 n = 365 t = 15 #calculate final amount P*(pow((1+r/n), n*t)) 16598.40198554521
This investment will be worth $16,598.40 after 15 years.
Additional Resources
The following tutorials explain how to perform other common tasks in Python:
How to Calculate Z-Scores in Python
How to Calculate Correlation in Python
How to Calculate a Trimmed Mean in Python