Menu

Arithmetic Operators

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.

The seven operators

SymbolNameWhat it doesExample
+additionadds the two values together11 + 4 gives 15
-subtractiontakes the second value away from the first11 - 4 gives 7
*multiplicationmultiplies the two values11 * 4 gives 44
/divisiondivides, keeping any fractional part11 / 4 gives 2.75
%modulogives the remainder left after whole-number division11 % 4 gives 3
//quotient (integer division)gives only the whole-number part of a division11 // 4 gives 2
**exponentiationraises the first value to the power of the second3 ** 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.

Writing an operation in Python

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 over

Output when the user types 26, 5:

How many apples? 26
Apples in each box? 5
5
1

The 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.

Practise this