*19*

You can use the following function in Python to calculate the monthly payments necessary to pay off a certain loan, given the initial size of the loan, duration of the loan, and annual interest rate:

(rate/12) * (1/(1-(1+rate/12)**(-months)))*P

The following examples show how to use this function in different scenarios.

**Example 1: Calculate Loan Payments for Mortgage**

Suppose a family takes out a mortgage loan for a house with the following details:

- Mortgage Amount: $200,000
- Number of Months: 360
- Annual Interest Rate: 4%

We can use the following codeÂ to calculate the necessary monthly loan payment:

#define initial size of loan, duration of loan, and annual interest rate P = 200000 months = 360 rate = .04 #calculate monthly payment (rate/12) * (1/(1-(1+rate/12)**(-months)))*P 954.8305909309076

The monthly loan payment is **$954.83**. This is how much the family must pay each month in order to pay off the $200,000 loan in 360 months.

**Example 2: Calculate Loan Payments for Car Loan**

Suppose an individual takes out a loan for a car with the following details:

- Loan Amount: $20,000
- Number of Months: 60
- Annual Interest Rate: 3%

We can use the following codeÂ to calculate the necessary monthly loan payment:

#define initial size of loan, duration of loan, and annual interest rate P = 20000 months = 60 rate = .03 #calculate monthly payment (rate/12) * (1/(1-(1+rate/12)**(-months)))*P 359.3738132812698

The monthly loan payment is **$359.37**. This is how much the individual must pay each month in order to pay off the $20,000 loan in 60 months.

**Example 3: Calculate Loan Payments for Student Loan**

Suppose a student takes out a loan for university with the following details:

- Loan Amount: $40,000
- Number of Months: 120
- Annual Interest Rate: 5.2%

We can use the following codeÂ to calculate the necessary monthly loan payment:

#define initial size of loan, duration of loan, and annual interest rate P = 40000 months = 120 rate = .052 #calculate monthly payment (rate/12) * (1/(1-(1+rate/12)**(-months)))*P 428.18316863206525

The monthly loan payment is **$428.18**. This is how much the individual must pay each month in order to pay off the $40,000 loan in 120 months.

**Additional Resources**

The following tutorials explain how to perform other common functions in Python:

How to Calculate Compound Interest in Python

How to Calculate Moving Averages in Python

How to Calculate Correlation in Python