Arithmetic operators
The seven operators, integer division and remainder, and the order they work in.
Do this lesson in the simulatorRobots 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
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)
Turning seconds into minutes and seconds, splitting sweets between a class, telling odd from even: // and % are more useful than they look.
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))
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)
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())
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
- Print the distance ahead in metres instead of centimetres.
- Ask for a number of seconds with
inputand print it as minutes and seconds. - Try
-7 // 2and-7 % 2. The answers surprise most people. Work out the rule Python is using.