Skip to main content

Section 13.2 Compute with Numbers

Computers were first used for numeric calculations. You may be used to doing calculations with a calculator, but calculations are often easier if you can name the numbers you are working with. When you name a number or the result of a calculation, you are creating a variable. A variable is a name associated with computer memory that can hold a value and that value can change or vary. One example of a variable is the score in a game. The score starts at 0 and increases as you play the game.
Figure 13.2.1. Both the score and high score in the game Angry Birds would be represented by a variable.
One thing that you might want to calculate is the monthly payment on a loan for a car. To do so, you can use this mathematical formula:
\begin{gather*} A = P \frac{r(1 + r)^n}{(1 + r)^n - 1} \end{gather*}
That formula uses the following variables:
P
The principle - the amount of the original loan
r
The monthly interest rate expressed as a decimal value. Loans are often described in terms of an APR or Annual Percentage Rate that must be divided by 1200 to produce the right value for r.
n
The number of monthly payments
It calculates the value A which is the payment amount.
The Python program below sets some values for these variables and then does the calculations needed to produce the payment. Press the Save & Run button below to make the computer execute these steps. The output from this program will be displayed to the right of the program.
Currently the code is calculating the payment for a $10,000 loan at an APR of 4.9% over 60 months. Try changing the numeric values for P, APR, and n in the program above, and press the Run button to calculate the payment for a different loan. Note that some of the symbols used in Python are different than those we normally use for math: * means “multiply” and ** means “to the power of”.
Also visible in the code are some comments. Comments are pieces of text that come after a # symbol, like # $10,000 - notice we don't use commas in numbers. Python will ignore these comments and they are colored differently than the code to indicate that they are not actual code. Comments are used by programmers to leave notes to themselves and others about the code. Try deleting a # in the program above and then running it. You will get an error message because now Python is trying to run the note as if it were code.

Note 13.2.2.

Notice how naming the values (using variables) for things like n makes it easier to see which values to change to make the program calculate a different loan.

Checkpoint 13.2.3.

    Try calculating the payment for an $8,000 loan at 6.5% interest over 72 months. What is the monthly payment?
  • 134.48
  • That would be the right dollar amount, but the computer will give you more digits than that.
  • 134.4794369902589
  • Yes!
  • 134
  • No, the computer does not round the result.
  • 135
  • No, the computer does not round the result.

Checkpoint 13.2.4.

Try calculating the payment for a $20,000 loan at 7% interest over 48 months. What is the monthly payment?
You have attempted of activities on this page.