teachinTech


Calculations in Python

One of the earliest use of computers was to do calculatations. In this activity, we will do some calculations in Python.

Turn In: Screenshot of console session

Python uses + and - for addition and subtraction, * for multiplication, and / for division. Exponentation is done with ^ while integer division is // and the remainder is calculated with the % symbol.

Calculations follow operator precedence. When Python does the calculation it works from left to right doing all ^ first, then next all * / // % operations, and finally the + - items. If there are () for grouping, those are done first. More details on the operator precedence can be found in the Python documentation link below.

Operator precedence

The best way to learn how calculations work is to do them and observe the results.

Start a Python Console Session

There are a few ways to run a Python console session, two of them are provided below. Choose one or the other (or you can try both).

1. Start PyCharm. Often the Python console window is on the bottom area of PyCharm. If you do not see the Python console, click on View then Tool Windows then Python console.

2. Start a command prompt (hold the Windows key then press R, type in cmd, then click ok). At the command prompt, type in the following (assuming you have Python 3.5 installed, if not change the 35 to the appropropriate number). "c:\Program Files\Python35\python.exe"

When finished with the Python console, type in exit() or press Ctrl-Z then hit the Enter key to exit

The console prompt is three greater than signs ... >>>

Do Some Calculations

In the Python console, we can just type in calculations and get the results back immediately. Try the calculations below (they will have >>> in front of them but do not type in the >>>) and guess what you think the answer will be. A description will follow the calculation. Try a few calculations on your own.

>>> 6 - 5 + 7 - 3

Do from left to right since + - are the same precedence. 6 - 5 is 1. 1 + 7 is 8. 8 - 3 is 5. 5 is the answer.

>>> 23 - 4 * 5

* / is done before + - so first do 4 * 5 which is 20. Then 23 - 20 for a final answer of 3.

>>> 20 * 2 ^ 3 - 24 // 4 + 9

^ is done first so 2 ^ 3 is 2 * 2 * 2 (2 to the 3rd) or 8. Next is * / from left to right. 20 * 8 is 160. Next is 24 // 4 or 6 (just one / would be a real number and give us an error). At this point we have 160 - 6 + 9. + - from left to right would be 160 - 6 or 154 then subtract 9 for a final answer of 145.

>>> 20 // 3 + 20 % 3

Think back to elementary school and long division with remainder. // and % are done first. 20 // 3 is 6 (3 goes into 20 6 times). 20 % 3 is 2 since 3 goes into 20 6 times with a remainder of 2. 6 + 2 is 8.

>>> (3 + 4) * (5 + 6) * (9 - 8)

First, do all things in parenthesis. (3 + 4) is 7, (5 + 6) is 11, and (9 - 8) is 1. Next, 7 * 11 * 1 is 77.

>>> 1 - 2 * (3 + 4) // 5 % 6

() first so (3 + 4) or 7. * // % from left to right ... 2 * 7 or 14 then 14 // 5 which is 2 then 2 % 6 which is 2. 1 - 2 is -1.

Be sure to screenshot your Python console window and feel free to try any additional calculations on your own.


Copyright © 2021 Eric Schumm. Permission granted to freely use this in your classroom.