Calculate Compound Interest in Python

Calculate compound interest in Python

Lets see how to calculate compound interest in Python! Very simple and straight forward.

principal = 100000

yearly_rate = 0.05

years = 30
yearly_addition = 10000
# calculate the principal and interest for each year
data = []
for i in range(1, years+1):
    principal = principal + yearly_addition
    interest = principal * yearly_rate
    principal = principal + interest
    data.append(principal)
    print(f"Year {i} - Principal: {principal}, Interest: {interest}")

Below code will calculate the monthly payment

# calculate the monthly payment
n = years * 12
r = interest_rate/12
monthly_payment = (principal * r) / (1 - (1 + r)**-n)
print(monthly_payment)