- Kalpesh Agrawal

# Annuity Calculator in Python

When you are working in corporates calculating basic annuities in a calculator is not advisable. **So today we are going to learn how to calculate annuity in python.**

**"BASIC PYTHON KNOWLEDGE IS MUST FOR THIS"**

You have to simply add inputs and it automatically calculates the annuity factor for you.

I have divided the process of creating an annuity calculator in 4 Steps.

**STEP 1:**

Creating a calculator for an annuity in arrears:

`Define an object and include interest rate , term , and accumulation factor if you want.`

`create a for loop to calculate and add annuity factor for each year in the variable a. `**a****+=**** is a shorthand operation ****for**** writing a ****=**** a****+**

`Now use if else to return the value stored in a. Accumulate will calculate future value of annuity if you set it to TRUE. `**Round ****function**** returns answer to ****5**** significant digits****.**

**def ****annuity_arrear**** ****(****int_rate****,**** n ****,**** accumulate ****=**** False****)****:**
**a** = **0**
**for**** i ****in**** ****range** (**0**, n):
** a ****+=**** ****(****1**** ****+**** int_rate****)********** ****-**** ****(****i ****+**** ****1****)****
****if**** accumulate ****==**** False****:****
****return**** ****round**** ****(****a****,**** ****5****)****
****else****:****
****return**** ****round****(****a ********* ****(****(****1**** ****+**** int_rate****)**********n****)****,**** ****5****)****
**

__STEP 2 :__

Creating a calculator for an annuity in due:

`Everything remains same and the only thing that changes here is in 4th line. `

**def ****annuity_due**** ****(****int_rate****,**** n ****,**** accumulate ****=**** False****)****:**
**a** = **0**
**for**** i ****in**** ****range** (**0**, n):
** a ****+=**** ****(****1**** ****+**** int_rate****)********** ****(****-****i ****)****
****if**** accumulate ****==**** False****:****
****return**** ****round**** ****(****a****,**** ****5****)****
****else****:****
****return**** ****round****(****a ********* ****(****(****1**** ****+**** int_rate****)**********n****)****,**** ****5****)**

__STEP 3 : __

Creating a calculator for increasing Annuity in arrears:

**def ****inc_annuity_arrear****(****int_rate****,**** n ****,**** accumulate ****=**** False****)****:**
**a** = **0**
**for**** i ****in**** ****range** (1 , n+1):
** a ****+=**** i ********* ****(****1**** ****+**** int_rate****)********** ****(****-****i ****)****
****if**** accumulate ****==**** False****:****
****return**** ****round**** ****(****a****,**** ****5****)****
****else****:****
****return**** ****round****(****a ********* ****(****(****1**** ****+**** int_rate****)**********n****)****,**** ****5****)**

Step 4 :

Creating a Calculator for Increasing Annuity in due :

**def ****inc_annuity_due****(****int_rate****,**** n ****,**** accumulate ****=**** False****)****:**
**a** = **0**
**for**** i ****in**** ****range** (0, n):
** a ****+=**** ****(****1**** ****+**** i****)**** ********* ****(****1**** ****+**** int_rate****)********** ****(****-****i ****)****
****if**** accumulate ****==**** False****:****
****return**** ****round**** ****(****a****,**** ****5****)****
****else****:****
****return**** ****round****(****a ********* ****(****(****1**** ****+**** int_rate****)**********n****)****,**** ****5****)**

__STEP 5 : __

Combining all the calculators created and creating a master calculator

```
Import pandas library to use series feature of it
Now call all the calculators we have created by creating a dictionary
```

```
import pandas as pd
def annuity_calculator(int_rate , n):
df = pd.series({"PV_annuity_arrear": annuity_arrear(int_rate , n),
"PV_annuity_due": annuity_due(int_rate , n),
"AV_annuity_arrear": annuity_arrear(int_rate,n ,accumulate = True),
"AV_annuity_due": annuity_due(int_rate , n ,accumulate = True),
"PV_inc_annuity_arrear": inc_annuity_arrear(int_rate, n),
"PV_inc_annuity_due": inc_annuity_due(int_rate , n),
"AV_inc_annuity_arrear": inc_annuity_arrear(int_rate,n , accumulate = True),
"AV_inc_annuity_due": inc_annuity_due(int_rate , n, accumulate = True)})
return df
```

I hope you understand well, how to make an annuity calculator in python.

`Related: What is Data Mining & The 9 Laws of Data Mining`

Written By: **Tirth Shah**

About the author: **Tirth Shah has cleared the CT series of Actuarial Science from IFOA. He completed his graduation in BCOM from Ahmedabad University in 2019.**

Email-ID**: **__tirthshah381@gmail.com__