Arithmetic Operators
An arithmetic operator is a symbol that tells the computer to carry out a calculation on two values. In Python the operator is written between the two values, and the computer works out a single result from them. There are seven arithmetic operators to recognise and use.
| Symbol | Name | What it does | Example |
|---|---|---|---|
+ | addition | adds the two values together | 11 + 4 gives 15 |
- | subtraction | takes the second value away from the first | 11 - 4 gives 7 |
* | multiplication | multiplies the two values | 11 * 4 gives 44 |
/ | division | divides, keeping any fractional part | 11 / 4 gives 2.75 |
% | modulo | gives the remainder left after whole-number division | 11 % 4 gives 3 |
// | quotient (integer division) | gives only the whole-number part of a division | 11 // 4 gives 2 |
** | exponentiation | raises the first value to the power of the second | 3 ** 4 gives 81 |
The three operators that divide all start from the same division but hand back different parts of it: / gives the complete answer, // gives the whole-number part and % gives what is left over. When one number divides into another exactly, % gives 0 because nothing is left over.
An operation needs two values, one on each side of the symbol, and it always produces one result. You must key in the Python symbol itself, so 12 * 3 works but 12 × 3 does not, and ^ does not mean "to the power of" in Python.
Because an operation produces a single value, you can store that result in a variable or print it. This program fills boxes with apples and uses both dividing operators on the same calculation:
apples = int(input("How many apples? "))
per_box = int(input("Apples in each box? "))
print(apples // per_box) # full boxes
print(apples % per_box) # apples left overOutput when the user types 26, 5:
How many apples? 26
Apples in each box? 5
5
1The first print shows how many full boxes you can fill, because // throws away anything left over. The second shows the spare apples that do not make a complete box, because % keeps only the remainder. Both lines come from one division, so the operator you choose depends on the part of the answer you actually need.
OCR GCSE Computer Science · Arithmetic Operators
OCR GCSE Computer Science · Arithmetic Operations