05-arithmetic-operators.py
python-programming / programming-foundations  ·  Lesson 05
Lesson 05 — Programming foundations

Arithmetic Operators

Use Python symbols for calculations, including DIV and MOD equivalents.

Arithmetic operators perform calculations. The important part is knowing both the general operator and the Python symbol that performs the same job.

Predict

Read the code first. Do not run it yet.

arithmetic-operators-predict.py
a = 17
b = 5
print(a // b)
print(a % b)
print(2 ** 4)
Predict before running
  • Predict all three outputs.
  • Which line gives the quotient?
  • Which line gives the remainder?

Investigate

Use this code to explain exactly how the program works.

arithmetic-operators-investigate.py
total = 23
boxes = 4
full_boxes = total // boxes
left_over = total % boxes
print(full_boxes, left_over)
Investigate the code
  • What do full_boxes and left_over represent?
  • Why is // more suitable than / for full boxes?
  • How are DIV and MOD represented in Python?

Operator comparison

OperationGeneral formPythonExample
Addition++7 + 2 → 9
Subtraction--7 - 2 → 5
Multiplication**7 * 2 → 14
Division//7 / 2 → 3.5
Integer quotientDIV//7 // 2 → 3
RemainderMOD%7 % 2 → 1
Exponentiation^**2 ** 3 → 8
Do not use ^ for powers in PythonIn algorithm notation, ^ can mean exponentiation. In Python, exponentiation is **; ^ has a different meaning.

A complete calculation

bill.py
price = 8.50
quantity = 3
total = price * quantity
print(total)
Output
25.5
How it works
  • * multiplies the two numeric values.
  • The result is stored in total before it is printed.

Self-study guide

Arithmetic operators let programs calculate new numeric values. Most symbols match between OCR Exam Reference Language and Python, but three differences are especially important: OCR DIV becomes Python //, OCR MOD becomes Python %, and OCR exponentiation ^ becomes Python **.

Do not use Python ^ for powers. In Python, ^ is a bitwise XOR operator, so it is not the GCSE exponentiation operator you are trying to express.

OCR focusRecognise and use addition, subtraction, multiplication, division, MOD, DIV and exponentiation. You must be able to translate the OCR operator names/symbols into correct Python.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
++Addition.
--Subtraction.
**Multiplication.
//Division.
DIV//Whole-number quotient / floor division for the positive GCSE-style examples used here.
MOD%Remainder after division.
^**Exponentiation. Python ^ is not exponentiation.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

DIV and MOD are easiest to understand as “complete groups” and “left over”.

lesson-05-worked.py
items = 29
box_size = 6

full_boxes = items // box_size
left_over = items % box_size

print(full_boxes)
print(left_over)
Output
4
5
Work through it line by line
  • 29 items can fill 4 complete groups of 6.
  • 29 // 6 therefore gives 4.
  • 4 groups use 24 items, leaving 5.
  • 29 % 6 therefore gives 5.
  • OCR would describe these operations as 29 DIV 6 and 29 MOD 6.

Another example

Exponentiation is another important OCR/Python difference.

lesson-05-example-2.py
level = 4
power = level ** 3
print(power)
Output
64
Why this example matters
  • Python 4 ** 3 means 4 × 4 × 4.
  • OCR ERL would write the exponent operation using ^.

Common mistakes

Avoid these errors
  • Writing ^ in Python when you mean “to the power of”.
  • Confusing / with //. / gives ordinary division; // gives a whole-number quotient for these positive integer examples.
  • Using % when you need the quotient rather than the remainder.
  • Ignoring normal precedence rules; use brackets when you need to make the intended order explicit.
Exam-safe warningExam comparison to memorise: OCR DIV → Python //; OCR MOD → Python %; OCR ^ → Python **.

Exam-ready checklist

You should now be able to
  • Select the correct arithmetic operator from the meaning of a problem.
  • Translate DIV, MOD and exponentiation correctly between OCR ERL and Python.
  • Trace calculations using operator precedence.
  • Use quotient and remainder together in grouping/time-conversion problems.

What you have learned

  • Use + - * / for the common arithmetic operations.
  • DIV corresponds to Python //.
  • MOD corresponds to Python %.
  • Exponentiation uses ** in Python.