• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Calculate Compound Interest in Python

Calculate Compound Interest in Python

June 5, 2022 Leave a Comment

To calculate compound interest in Python, you can use the formula to calculate compound interest and create a function.

def compound_interest(p,r,n,t):
    a = p*(1+r/100/n)**(n*t)
    return a - p

print(compound_interest(1000,5,1,10)) 

#Output:
628.894626777442

If you have continuous compounding and want to calculate the compound interest, then you can use the continuous compounding equation.

import math

def compound_interest(p,r,t):
    a = p * math.exp(r/100*t)
    return a - p

print(compound_interest(1000,5,10)) 

#Output:
648.7212707001281

Compound interest is the eighth wonder of the world, according to Albert Einstein.

The ability for us to calculate compound interest is valuable and with Python, you can easily create a function which will calculate compound interest and the total amount gained after a certain period of compounding.

The equation for periodic compounding is as follows.

amount = principal * (1 + rate / number of periods) ^ (number of periods * time)

If you want to get compound interest from this equation, you can subtract the starting principal from amount and get the total interest accrued.

In Python, this is easy to implement because it is just multiplying and dividing.

Below is an example showing you how to perform periodic compounding and calculate the compound interest in Python.

def compound_interest(p,r,n,t):
    a = p*(1+r/100/n)**(n*t)
    return a - p

print(compound_interest(1000,5,1,10))  #annually
print(compound_interest(1000,5,2,10))  #biannually
print(compound_interest(1000,5,4,10))  #quarterly
print(compound_interest(1000,5,12,10)) #monthly


#Output:
628.894626777442
638.6164402903942
643.6194634870103
647.0094976902801

Calculating Compound Interest in Python for Continuous Compounding

Another case of compounding is when you have continuous compounding. The equation for continuous compounding is as follows.

amount = principal * e ^ (rate * time)

In Python, this is easy to implement with the help of the math module.

Below is an example showing you how to perform continuous compounding and calculate the compound interest in Python.

import math

def compound_interest(p,r,t):
    a = p * math.exp(r/100*t)
    return a - p

print(compound_interest(1000,5,10)) 

#Output:
648.7212707001281

Hopefully this article has been useful for you to learn how to calculate compound interest in Python.

Other Articles You'll Also Like:

  • 1.  Convert First Letter of String to Lowercase in Python
  • 2.  PROC FREQ Equivalent in Python
  • 3.  Comparing Datetimes in Python
  • 4.  Using Python to Increment Dictionary Value
  • 5.  pandas drop – Drop Rows or Columns from DataFrame
  • 6.  Python divmod() Function – Get Quotient and Remainder After Division
  • 7.  Python Prepend to List with insert() Function
  • 8.  Multiply Each Element in List by Scalar Value with Python
  • 9.  Are Tuples Mutable in Python? No, Tuples are not Mutable
  • 10.  Selenium maximize_window() Function to Maximize Window in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy