Calculate Your Monthly Mortgage Using Python

randerson112358
3 min readOct 5, 2023

Disclaimer: The material in this article is purely educational and should not be taken as professional investment advice. Invest & budget at your own discretion. Affiliate links are in this article (by clicking on these links you help me out with no additional cost to yourself). Please enjoy the article!

Let’s create a Python program that calculates the monthly mortgage payment. This program will take inputs such as loan amount, interest rate, and loan term, and calculate the monthly payment amount based on the provided information. Here’s the code:

def calculate_monthly_payment(loan_amount, interest_rate, loan_term):
# Formula for calculating the monthly mortgage payment:
# M = (P * r * (1 + r)^n) / ((1 + r)^n - 1)
# Where:
# M = monthly payment
# P = loan amount
# r = monthly interest rate
# n = total number of monthly payments

# Converting interest rate to monthly rate
monthly_interest_rate = interest_rate / 100 / 12

# Converting loan term to total number of monthly payments
total_payments = loan_term * 12

# Calculating monthly mortgage payment
monthly_payment = (loan_amount * monthly_interest_rate * (1 + monthly_interest_rate) ** total_payments) / \
((1 + monthly_interest_rate) ** total_payments - 1)

return monthly_payment

print("Welcome to the Mortgage…

--

--