• 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
  • Ruby
  • 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.  Print Object Attributes in Python using dir() Function
  • 2.  Python Get Operating System Information with os and platform Modules
  • 3.  Sort Files by Date in Python
  • 4.  pandas startswith() – Check if String Starts With Certain Characters
  • 5.  pandas nlargest – Find Largest Values in Series or Dataframe
  • 6.  Using Python to Calculate Average of List of Numbers
  • 7.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 8.  Count Letters in Word in Python
  • 9.  pandas mean – Get Average of Series or DataFrame Columns
  • 10.  Scroll Up Using Selenium 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy