Arithmetic operators

The seven operators, integer division and remainder, and the order they work in.

F1.9Programming basicsGCSE15 min

Covers OCR J277 2.2.1 Programming fundamentals · AQA 8525 3.2.3 Arithmetic operations in a programming language · Edexcel 1CP2 6.5.1 Arithmetic operators

Do this lesson in the simulator

Robots are numbers all the way down: speeds, distances, angles, battery percentages. The symbols that do arithmetic are called operators, and this lesson is where you make Python do the working out for you.

The seven operators

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print(17 + 5)     # addition: 22
print(17 - 5)     # subtraction: 12
print(17 * 5)     # multiplication: 85
print(17 / 5)     # division: 3.4
print(17 // 5)    # integer division: 3
print(17 % 5)     # modulus, the remainder: 2
print(5 ** 2)     # exponent, 5 to the power 2: 25

Run this in the simulator

The first four are the ones from maths, with * for times and / for divide. The last three are new.

Integer division and remainder

17 // 5 asks how many whole fives fit into 17: three. 17 % 5 asks what is left over: two. Together they are the division you did at primary school, 17 divided by 5 is 3 remainder 2.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

seconds = 135
print(seconds // 60, "minutes and", seconds % 60, "seconds")

# % 2 is 0 for even numbers and 1 for odd ones
print(8 % 2, 9 % 2)

Run this in the simulator

Turning seconds into minutes and seconds, splitting sweets between a class, telling odd from even: // and % are more useful than they look.

Number lines: 17 holds three whole jumps of 5 with 2 left over; 135 holds two whole jumps of 60 with 15 left over05101517jump 1jump 2jump 32 left17 // 5 = 3whole jumps of 517 % 5 = 2what is left over060120135jump 1jump 215 left135 // 60 = 2whole jumps of 60135 % 60 = 15what is left over
Division as jumps along a number line. // counts the whole jumps that fit, % measures the bit left before the end. For 135 seconds that is 2 whole minutes, and 15 seconds left.

Division always gives a real number

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print(20 / 5)
print(20 // 5)
print(10 / 3)
print(round(10 / 3, 2))

Run this in the simulator

20 / 5 printed 4.0, not 4: / always gives a float, even when the answer is whole. // gives an integer. round tidies a long float: the second argument is how many decimal places to keep.

Order of operations

Python follows the same order as maths: brackets first, then exponents, then multiply and divide (including // and %), then add and subtract.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 * 3 ** 2)

Run this in the simulator

Predict all three before running. When in doubt, add brackets: they never hurt, and they show the reader what you meant.

Arithmetic with the robot

The robot's answers are numbers, so they can go straight into a calculation, and a calculation can go anywhere a function wants a number:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

side = 20
print("a square of side", side, "cm covers", side ** 2, "square cm")

ahead = distance()
print("wall in", ahead, "cm; half way is", ahead / 2)
forward(50, distance=ahead / 2)
print("at", position())

Run this in the simulator

The drive went half the way to whatever was ahead. Numbers from sensors feeding into actions: that is most of robotics.

Task: robot maths

Using only the numbers 17 and 5 in your calculations, print four lines: divide: 3.4, DIV: 3, MOD: 2 and power: 25 (the power is 5 to the power 2). Work each one out with an operator; do not type the answers. The robot must not drive.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("divide:", 17 / 5)

Task: a third of the way

There is a wall ahead. Measure the distance to it and drive exactly a third of that distance forward. The program has to work out the third itself, so do not type in a distance.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

ahead = distance()

Challenges

  1. Print the distance ahead in metres instead of centimetres.
  2. Ask for a number of seconds with input and print it as minutes and seconds.
  3. Try -7 // 2 and -7 % 2. The answers surprise most people. Work out the rule Python is using.

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.