Python Lab · Experiment 2
Use the Python interpreter as a calculator
PythonWeek 1JNTUH R22 · CS206ES
Aim
To use Python as a calculator by evaluating expressions with the arithmetic operators + − * / // % and **, and to see how operator precedence and brackets change the result.
Algorithm
- Start the interpreter by typing
python(orpython3on Linux) in a terminal. The>>>prompt appears. - Type an expression such as
7 + 2and press Enter. The interpreter prints the value on the next line. - Try each arithmetic operator: addition, subtraction, multiplication, true division
/, floor division//, remainder%and power**. - Try expressions that mix operators, with and without brackets, to see the order in which Python works them out.
- To keep a record, write the same expressions in a program and print each one with its value, as in the program below.
Program
# Basic arithmetic operators
print("7 + 2 =", 7 + 2)
print("7 - 2 =", 7 - 2)
print("7 * 2 =", 7 * 2)
print("7 / 2 =", 7 / 2) # true division, always gives a float
print("7 // 2 =", 7 // 2) # floor division, drops the fraction
print("7 % 2 =", 7 % 2) # remainder
print("7 ** 2 =", 7 ** 2) # power
print()
# Floor division and remainder with a negative number
print("-7 // 2 =", -7 // 2)
print("-7 % 2 =", -7 % 2)
print()
# Operator precedence
print("2 + 3 * 4 =", 2 + 3 * 4)
print("(2 + 3) * 4 =", (2 + 3) * 4)
print("10 - 4 - 3 =", 10 - 4 - 3)
print("2 ** 3 ** 2 =", 2 ** 3 ** 2)
print("-3 ** 2 =", -3 ** 2)
print("100 / 10 * 2 =", 100 / 10 * 2)
print()
# A few practical calculations
print("Area of a circle of radius 5 =", 3.14159 * 5 ** 2)
print("Average of 72, 85 and 90 =", (72 + 85 + 90) / 3)
print("Big power 2 ** 100 =", 2 ** 100)Output
7 + 2 = 9
7 - 2 = 5
7 * 2 = 14
7 / 2 = 3.5
7 // 2 = 3
7 % 2 = 1
7 ** 2 = 49
-7 // 2 = -4
-7 % 2 = 1
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
10 - 4 - 3 = 3
2 ** 3 ** 2 = 512
-3 ** 2 = -9
100 / 10 * 2 = 20.0
Area of a circle of radius 5 = 78.53975
Average of 72, 85 and 90 = 82.33333333333333
Big power 2 ** 100 = 1267650600228229401496703205376Recorded by running this exact program.
How it works
The task asks you to type expressions at the interpreter's >>> prompt. That live session cannot be recorded here, so the program prints the same expressions. At the prompt you would type only the expression, for example 7 // 2, and the interpreter would print 3 on the next line. You do not need print there, because the interpreter shows the value of every expression you type.
Order of precedence, from highest to lowest: brackets, then **, then unary minus, then *, /, //, %, then + and -. Operators of the same level are worked out from left to right, except **, which goes from right to left. That is why 2 ** 3 ** 2 is 2 ** 9 = 512, and -3 ** 2 is -(3 ** 2) = −9.
// rounds down towards minus infinity, not towards zero, so -7 // 2 is −4. The remainder % then takes the sign of the divisor, so -7 % 2 is 1. In both cases (a // b) * b + a % b == a.
Python integers have no fixed size, so 2 ** 100 is printed exactly.
Common mistakes
Expecting
7 / 2to give 3, as in C. In Python 3,/always gives a float (3.5). Use//when you want the whole-number part.Using
^for power. In Python^is bitwise XOR, so2 ^ 3is 1. Power is written**.Forgetting brackets:
2 + 3 * 4is 14, not 20, because*is done before+. Add brackets when you mean a different order.Typing an expression without
printin a script file and expecting to see the value. Only the interactive prompt shows values on its own; a script must callprint.
Viva questions
Try to answer each one aloud before you open it.
What is the difference between / and // in Python?
/is true division and always gives a float, so7 / 2is 3.5.//is floor division and gives the largest whole number not greater than the result, so7 // 2is 3 and-7 // 2is −4.What does the % operator do?
It gives the remainder of a division.
7 % 2is 1. It is often used to test whether a number is even (n % 2 == 0).What is the value of 2 ** 3 ** 2 and why?
- The
**operator is worked out from right to left, so it is2 ** (3 ** 2), which is2 ** 9.
- The
What is operator precedence?
It is the rule that decides which operator is applied first when an expression has several. For example
*has higher precedence than+, so2 + 3 * 4is 14.Is there a limit to the size of an integer in Python?
No fixed limit. Python integers grow as large as memory allows, so
2 ** 100is calculated exactly.