Python Foundations Lesson 3 of 7

Python numbers and arithmetic

20 min, 5 exercisesOCR 2.2.1AQA 3.2.3Edexcel 6.5.1

Builds on lessons 1 to 2. Start at lesson 1

Learn

Int, float and operators

Python has two everyday kinds of number: int (whole numbers like 7 or -3) and float (numbers with a decimal point like 2.5).

The operators:

  • + - * do what you expect.
  • / is normal division and always gives a float: 6 / 2 is 3.0, not 3.
  • // is floor division: divide, then round down. 7 // 2 is 3.
  • % is the remainder (often called modulo): 7 % 2 is 1. Super handy: n % 2 == 0 means n is even.
  • ** is power: 2 ** 10 is 1024.

Learn

Order and float surprises

Python follows the usual order: ** first, then * / // %, then + -. Use brackets when in doubt, so (2 + 3) * 4 is 20.

Careful with negatives: // rounds down, towards minus infinity, so -7 // 2 is -4, not -3.

Now the famous surprise:

print(0.1 + 0.2)
OUTPUT 0.1 + 0.2
print(0.1 + 0.2)   # 0.30000000000000004

Python is not broken. Computers store floats in binary, and 0.1 has no exact binary form, the same way 1/3 has no exact decimal form (0.3333...). The tiny error shows up when you print. Two lessons from this: never test floats with == when exactness matters, and use round(x, 2) when you want a tidy result.

Ints, on the other hand, are exact and have no size limit. 2 ** 100 gives you all 31 digits, no problem.

GCSE exam boards
  • OCR 2.2.1 Programming fundamentals
  • AQA 3.2.3 Arithmetic
  • Edexcel 6.5.1 Arithmetic operators

Try

Run it: division and powers

The example is already in the editor. Run it, change one thing, and run it again.

example.py to run
print(7 / 2)     # 3.5   normal division, always a float
print(7 // 2)    # 3     floor division
print(7 % 2)     # 1     remainder
print(2 ** 10)   # 1024  power
print(-7 // 2)   # -4    rounds down, not towards zero

print(0.1 + 0.2)              # the classic float surprise
print(round(0.1 + 0.2, 2))    # tidy it up
print(2 ** 100)               # ints never overflow

Practice

Minutes and seconds

total is a number of seconds. Use // and % to work out the whole minutes and the leftover seconds, and print them so the output is exactly 200 seconds is 3 min 20 s. Your code should work for any value of total, so do not type 3 or 20 yourself.

Constraint

  • Work it out with code. Printing the answer as text doesn't count.
exercise_1.py to run
total = 200
minutes = 0
seconds = 0
print(total, "seconds is", minutes, "min", seconds, "s")

Practice

Share out pizza slices

Eight friends share slices = 30 slices. Print how many whole slices each person gets, then on the next line how many slices are left over. The output should be 3 on the first line and 6 on the second.

Constraint

  • Work it out with code. Printing the answer as text doesn't count.
exercise_2.py to run
slices = 30
people = 8
print(slices / people)

PracticePredict the output

Five kinds of division

Type exactly what this prints, one result per line. Careful with the last one.

exercise_3.py
print(9 / 3)
print(9 // 4)
print(9 % 4)
print(2 ** 3 * 2)
print(-9 // 4)

PracticeChange it

Add hours to the timer

This converts seconds into minutes and seconds. Change it so it also works out whole hours, and prints 3725 seconds is 1 h 2 min 5 s. It has to work for any total, so do not type 1, 2 or 5 yourself.

Constraint

  • Work it out with code. Printing the answer as text doesn't count.
exercise_4.py to run
total = 3725
minutes = total // 60
seconds = total % 60
print(total, "seconds is", minutes, "min", seconds, "s")

Your turn

Whose turn is it?

Four players take turns in a board game, and turns are numbered from 0: turn 0 is player 1, turn 1 is player 2, turn 4 is player 1 again. Every four turns make one round, starting at round 1. With turn = 14 and players = 4, print Turn 14 player 3 round 4. Work out the player and the round with % and //, so the program still works if you change turn.

Constraint

  • Use % for the player.
  • Use // for the round.

On your own. Hints open after your first check.

exercise_5.py to run
turn = 14
players = 4
Python 3.13

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.