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.

## Leave a Reply