Programming, Python
Mortgage Calculator Python Code

This code will teach the mathematical foundations of the mortgage calculator so that if you are considering a mortgage loan, you should understand all the details on how principal and interest is calculated on a mortgage loan.
An excellent mortgage calculator that you can use as a black box tool can be found here: Dave Ramsey Mortgage Calculator.
To be on top of your game, it is important that you understand how the mortgage calculator works so that you are not using this tool as a black box. A mortgage is the biggest loan of your life. Avoiding mistakes when applying for a mortgage loan can save you a lot of money.
Important Remark: This code can be used for calculating monthly principal and interest payments ONLY. The actual monthly payment for a mortgage loan will include other fees such as:
- HOA (Home Owners Association fee)
- PMI (Private Mortgage Insurance fee)
- Home Owners Insurance
- Taxes
Mathematical Derivations

Month 1 Calculations:

Month 2 Calculations:

Month 3 Calculations:

Month n (n≥1) Calculations:

Pseudocode for Mortgage Loan Calculator

Implementation Using Python
Import Necessary Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pdMain Code
Sales_Price = float(input('Enter sales price of house in US dollars: '))Down_Payment = float(input('Enter down payment as a percentage of Sales Price, e.g. 5 for 5%: '))Loan_Amount = Sales_Price*(1-Down_Payment/100)Mortgage_Type = float(input('Enter mortgage type in years, e.g 15 for 15 years: '))Loan_Term = int(12*Mortgage_Type)Interest_Rate = float(input('Enter loan interest rate as a percentage, e.g 4 for 4%: '))R = 1 +(Interest_Rate)/(12*100)X = Loan_Amount*(R**Loan_Term)*(1-R)/(1-R**Loan_Term)Monthly_Interest = []
Monthly_Balance = []for i in range(1,Loan_Term+1):
Interest = Loan_Amount*(R-1)
Loan_Amount = Loan_Amount - (X-Interest)
Monthly_Interest = np.append(Monthly_Interest,Interest)
Monthly_Balance = np.append(Monthly_Balance, Loan_Amount)Print Useful Outputs
print("The Home Sales Price is: = " + str('$')+ str(Sales_Price))print("The Down Payment as a Percentage of Sales Price is: = " + str(Down_Payment)+str(' %'))print("The Loan Amount is: = " + str(Sales_Price*(1-Down_Payment/100))+str(' %'))print("The Interest Rate on Annual Percentage Basis is: = " + str(Interest_Rate)+str(' %'))print("The duration of this loan, that is the Loan Term (in months) is: = " + str(Loan_Term)+str(' months'))print("Monthly Payment for this Mortgage(P & I) is: = " + str('$')+str(np.round(X,2)))print("Total interest paid over life cycle of the loan is: = " + str('$') + str(np.round(np.sum(Monthly_Interest),2)))Produce Visualization of Monthly Loan Balance and Interest
plt.plot(range(1,Loan_Term+1),Monthly_Interest, 'r',lw=2)
plt.xlabel('month')
plt.ylabel('monthly interest ($)')
plt.show()plt.plot(range(1,Loan_Term+1),Monthly_Balance,'b',lw=2)
plt.xlabel('month')
plt.ylabel('monthly loan balance ($)')
plt.show()Sample Output Using Mortgage Calculator Code
Assume House Sales Price in the amount of $275,000.

All calculations displayed on this table are consistent with the results obtained using this Online Mortgage Calculator: Dave Ramsey Mortgage Calculator.
In summary, we’ve shown how a mortgage calculator can be built using python. This tool can be used by anyone considering lending a mortgage loan from an authorized lender. The Jupyter Notebook file for this article can be downloaded from this repository: https://github.com/bot13956/Mortgage_Calculator.