Grade by Grade
A. Programming Concepts and Skills · Programming Basics: Data, Decisions and Loops

Arithmetic Expressions and the Order of Operations

A1.3 A1.4About 70 minutes
Learning goals
  • I can use all seven arithmetic operators correctly, including //, % and **.
  • I can evaluate an expression using Python's order of operations.
  • I can use integer division and modulo to solve problems such as converting minutes to hours and minutes.
  • I can translate a math formula into a correct Python assignment statement.
  • I can predict whether a result will be an int or a float, and round a float for display.

Words to know

expression
A combination of values, variables and operators that Python evaluates to a single value, such as price * 2 + 5.
operator / operand
An operator is a symbol for an operation (+, *). The operands are the values it works on.
integer division (//)
Division that gives the whole-number part of the quotient, rounded down: 17 // 5 is 3.
modulo (%)
The remainder after integer division: 17 % 5 is 2.
exponentiation (**)
Raising to a power: 2 ** 3 is 8.
precedence
The priority of an operator, which decides which part of an expression is evaluated first.

Python as a calculator

A snowplough route is 47 km long. The plough drives at 18 km/h. How many whole hours does it take, and how many extra kilometres are left in the last part-hour? A programmer answers that in one line each, using Python's arithmetic operators.

In this lesson you will learn every arithmetic operator in Python, the order Python uses them in, and how to turn formulas from math and science into code.

OperatorMeaningExampleResult
+addition7 + 29
-subtraction7 - 25
*multiplication7 * 214
/division (always gives a float)7 / 23.5
//integer (floor) division7 // 23
%modulo (remainder)7 % 21
**exponent (power)7 ** 249
Python's arithmetic operators. Use * for multiplication, never x or ×.

Integer division and modulo

These two belong together. When you divide whole numbers by hand, you get a quotient and a remainder:

17 ÷ 5 = 3 remainder 2

In Python, 17 // 5 gives the quotient 3 and 17 % 5 gives the remainder 2. They always fit together: (17 // 5) * 5 + 17 % 5 equals 17.

Back to the snowplough: 47 // 18 is 2 full hours, and 47 % 18 is 11 km still to plough in the third hour.

These two operators are surprisingly useful:

  • Converting units: 200 minutes is 200 // 60 = 3 hours and 200 % 60 = 20 minutes.
  • Even or odd: n % 2 is 0 for even numbers and 1 for odd numbers.
  • Divisibility: if n % 7 == 0, then n is a multiple of 7.
  • Last digit: n % 10 is the last digit of a positive whole number, and n // 10 removes the last digit (2026 % 10 is 6; 2026 // 10 is 202).
bag 1bag 2bag 3left over17 // 5 = 3 full bags17 % 5 = 2
Packing 17 cookies into bags of 5: 17 // 5 counts the full bags (3) and 17 % 5 counts the cookies left over (2). Check: 3 × 5 + 2 = 17.

Negative numbers with // and %

// rounds down (toward negative infinity), not toward zero. So -7 // 2 is -4 (because -3.5 rounds down to -4) and -7 % 2 is 1 (because -4 × 2 + 1 = -7). This is different from int(-3.5), which gives -3. With positive numbers you will not notice the difference.

Order of operations

You know BEDMAS from math class. Python follows the same idea, with a few extra operators:

  1. Parentheses first. Use them freely to make your meaning clear.
  2. Exponents, written **. Several exponents in a row are done right to left (see the program below).
  3. A negative sign in front of a value. Careful: -2 ** 2 is −4, because the exponent is done before the negative sign. Put −2 in parentheses to get (−2)² = 4.
  4. *, /, // and %, all at the same level, left to right.
  5. + and -, left to right: 10 - 4 - 3 is (10 - 4) - 3 = 3, not 10 − 1.
1( )parentheses: always first2**exponent (works right to left)3-xnegation (a minus sign in front)4* / // %multiply and divide (left to right)5+ -add and subtract (left to right)
Python's order of operations for arithmetic, from highest priority (done first) to lowest. Operators on the same row are done left to right, except **, which is done right to left.
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)
print(-2 ** 2)
print((-2) ** 2)

Exponents chain right to left: the first line is 2 to the power 9. Parentheses change the order. The negative sign is applied after the exponent. Output, line by line: 512 | 64 | -4 | 4

2 + 3 * 4 ** 2 // 5start2 + 3 * 16 // 5** first: 4 ** 2 = 162 + 48 // 5then left to right: 3 * 16 = 482 + 948 // 5 = 911last: 2 + 9 = 11
Evaluating 2 + 3 * 4 ** 2 // 5 one operation at a time. At each step, find the highest-priority operator that is left.

int or float?

  • int with int gives an int for +, -, // and %, and also for * and for powers with a zero or positive exponent.
  • / always gives a float: 8 / 2 is 4.0.
  • If either operand is a float, the result is a float: 7.0 // 2 is 3.0.

To show a float neatly, use round(value, digits): round(3.14159, 2) is 3.14. With no second argument, round gives an int: round(3.7) is 4. One surprise: when a value is exactly halfway, Python rounds to the even neighbour, so round(2.5) is 2 and round(3.5) is 4. (Lesson 7 shows another way to format output to 2 decimal places.)

Worked example

Example 1: Converting minutes

A video is 200 minutes long. Write code that prints it as 3 h 20 min.

  1. Hours are the number of full groups of 60: hours = total // 60, which is 200 // 60 = 3.
  2. Minutes are what is left over: minutes = total % 60, which is 200 % 60 = 20.
  3. Print the parts with commas: print(hours, "h", minutes, "min") puts one space between the items.
  4. Full program: - total = 200 - hours = total // 60 - minutes = total % 60 - print(hours, "h", minutes, "min")

3 h 20 min. Check: 3 × 60 + 20 = 200.

Worked example

Example 2: Formulas in code

Write Python assignments for (a) the area of a circle, A = πr², with r = 2.5 cm, and (b) the Fahrenheit temperature F = (9/5)C + 32 for C = -40.

  1. (a) Make a constant PI = 3.14159. Multiplication must be written with an asterisk, and r² is written with the exponent operator: area = PI * r ** 2. The exponent is done first, so this is π × (r²), as in the formula.
  2. With r = 2.5: 2.5 2 = 6.25, and 3.14159 × 6.25 = 19.6349375. round(area, 2) is 19.63**.
  3. (b) fahrenheit = 9 / 5 * celsius + 32. Left to right: 9 / 5 = 1.8, then 1.8 × (−40) = −72, then −72 + 32 = −40.0. (Yes, −40 is the same in both scales!)
  4. A common mistake is area = PI * r * 2 (that is the circumference formula, not the area) or 9 / (5 * celsius) + 32 (wrong grouping).

area = PI * r ** 2 gives about 19.63 cm²; fahrenheit = 9 / 5 * celsius + 32 gives -40.0.

PI = 3.14159
r = 2.5
area = PI * r ** 2
print(round(area, 2))
celsius = -40
fahrenheit = 9 / 5 * celsius + 32
print(fahrenheit)

Example 2 as a program. Output, line by line: 19.63 | -40.0

Just for fun

Python integers have no size limit. Type print(2 ** 1000) and Python instantly prints the exact answer: a number 302 digits long. Your calculator gives up long before that.

Hands-on activity

Egg cartons and remainders

You need: an empty egg carton (12 cups) or a muffin tin, and a handful of small items such as dried beans, buttons or coins (40 to 60 of them).

  1. Count your items. Call the number n.
  2. Fill cartons of 12 one at a time (reuse the same carton: fill it, record one full carton, empty it, repeat). Record how many full cartons you made and how many items were left.
  3. In Python, compute n // 12 and n % 12. Do they match?
  4. Repeat with groups of 5 (use only 5 cups) and compare with n // 5 and n % 5.
  5. Write a program that stores n and prints n as cartons and leftovers, like 52 eggs = 4 cartons + 4 extra.

What to look for: for 52 items, 52 // 12 is 4 and 52 % 12 is 4, and 4 × 12 + 4 = 52. One program: n = 52 then print(n, "eggs =", n // 12, "cartons +", n % 12, "extra").

Your turn

1

Evaluate: 20 // 6, 20 % 6, 20 / 6 (to 2 decimal places) and 2 ** 5.

2

Evaluate 5 + 2 * 3 ** 2.

3

Evaluate (5 + 2) * 3 ** 2.

4

Evaluate 100 - 30 - 20 and 100 / 10 / 2.

5

Write code that converts seconds = 500 into minutes and seconds.

6

Write a Python expression for the average of a, b and c.

7

Is the result an int or a float? 6 * 3, 6 / 3, 6.0 // 3, 7 % 2

More practice: warm-up

1

What is 15 % 4?

2

What is 15 // 4?

3

What is 3 ** 3?

4

What is 9 / 3?

5

How do you write 4 times x in Python?

More practice: on your own

1

Evaluate -3 ** 2. Then evaluate it again with the −3 in parentheses. Explain the difference.

2

Write an assignment for the hypotenuse c = √(a² + b²) without importing anything.

3

A number n is a positive 3-digit integer. Write expressions for its hundreds digit, tens digit and ones digit.

4

Evaluate 17 % 5 * 2 + 8 // 3.

5

A pizza is cut into 8 slices. Write code that, for people = 21 who each get one slice, computes how many whole pizzas are full, and how many extra slices are needed from one more pizza.

Challenge

1

A student writes x = 2 ** -1. What value and type does x get? Then explain why raising 2 to the power 0.5 does not give an int either.

2

What are -17 // 5 and -17 % 5? Show that they still fit (a // b) * b + a % b == a.

3

A total is cents = 1385. Write code that finds the fewest toonies ($2), loonies ($1), quarters (25¢), dimes (10¢) and nickels (5¢) to make it (cash totals in Canada are rounded to the nearest 5 cents because pennies are no longer distributed, so this total ends in 0 or 5).

Check your understanding

Quiz

1.

What is 23 % 4?

2.

What is 2 + 3 * 2 ** 2?

3.

What type is 10 / 2?

4.

Which expression correctly computes the average of x and y?

5.

What is -2 ** 4?

6.

Which pair converts m minutes into hours h and leftover minutes r?

7.

What is round(7.456, 1)?

For parents and teachers

Big idea: expectation A1.4 lists the arithmetic operators (including exponentiation and parentheses) and the order of operations; A1.3 asks students to use them in assignment statements.

How to help: give your teen an everyday conversion (days and weeks, cents and dollars, eggs and cartons) and ask for the two lines of Python using // and %. The egg carton activity makes the idea of a remainder concrete.

Common difficulties:

  • Writing 2x or 2(x + 1). Python needs 2 * x and 2 * (x + 1).
  • Using ^ for powers. In Python ^ means something else; use **.
  • Forgetting parentheses in averages and fractions: (a + b) / 2.
  • -2 ** 2 is −4, not 4.
  • Expecting / to give an int. Use // for whole-number division.