Python program: Simple and compound interest
Compare simple interest with compound interest on savings.
The code
principal = 10_000 # dollars rate = 0.05 # 5% a year years = 10 simple = principal * (1 + rate * years) compound = principal * (1 + rate) ** years monthly = principal * (1 + rate / 12) ** (12 * years) print(f"simple: ${simple:,.2f}") print(f"compound yearly: ${compound:,.2f}") print(f"compound monthly: ${monthly:,.2f}")
Output
simple: $15,000.00 compound yearly: $16,288.95 compound monthly: $16,470.09
How it works
Simple interest grows by the same amount each year; compound interest earns interest on the interest. 10_000 is the same as 10000 (underscores make big numbers readable); :,.2f adds thousands separators.
Python features used
Learn the ideas behind it in the free lesson Values, Variables & Numbers.