1. Python Basics1. Python Basics\1.1 Familiarizing Yourself with Python\1.1.4 A Useful Program

1.1.4 A Useful Program

 

In this section, you will use strings and numbers to make a useful program. This program calculates how much money you will pay on a loan with a certain interest rate.

 

If you borrow an amount of money X, at an annual rate R, and you pay it back over N months, there is an equation to calculate the monthly payment. It is a big, ugly equation:

 

 

 

This is a huge pain to enter into a calculator.

 

It is not very much fun to enter into Python, either, so just this once, you can copy and paste the calculation from the program below.

 

We are going to calculate a loan of $10,000 at 5.25 percent, for 72 months.

 

Create a new Python file, type the following and run it:

 

  1. print "This program calculates how much money you will pay on a loan."  
  2.   
  3. print "If you borrow 10000 dollars and pay it back over 72 months at an"  
  4. print "annual interest rate of 5.25 percent, your monthly payment will be:"  
  5. print (5.25 / 1200) * pow(1 + 5.25 / 1200, 72) * 10000 / (pow(1 + 5.25 / 1200, 72) - 1)  
  6.   
  7. print "Your total payable will be:"  
  8. print (5.25 / 1200) * pow(1 + 5.25 / 1200, 72) * 10000 / (pow(1 + 5.25 / 1200, 72) - 1) * 72  

 

You should see this output:

 

This program calculates how much money you will pay on a loan.

If you borrow 10000 dollars and pay it back over 72 months at an

annual interest rate of 5.25 percent, your monthly payment will be:

162.211537477

Your total payable will be:

11679.2306983

 

Congratulations! You have written a program that can do something useful for you. In the next section, we will modify it to make it a little more powerful.

 

 

Top of Page