Arithmetic Operators
In Python 3, four of the arithmetic operators do everyday maths: addition, subtraction, multiplication and division. Each one is a single symbol, and the computer works out the calculation when it runs the line of code.
| Calculation | Symbol | Example |
|---|---|---|
| Addition | + | sweets = 14 + 9 |
| Subtraction | - | sweets = 14 - 9 |
| Multiplication | * | sweets = 14 * 9 |
| Division | / | sweets = 14 / 9 |
Multiplication uses the asterisk * because a keyboard has no multiplication sign. Python cannot read 14 x 9, so typing x instead of * stops the program with a syntax error. Always type *.
Subtraction can give a negative result. If a shop has 26 chocolate bars and sells 31, then bars = 26 - 31 stores the value -5. A negative value is a perfectly valid number, so the program keeps running. The minus sign shows the value is below zero.
Division with / gives a result that may have a fractional part. Sharing 45 stickers between 8 players gives 45 / 8, which is 5.625. This is not the same as integer division //, which keeps only the whole-number part and gives 5. Even when the numbers divide exactly, / still answers with a decimal: 20 / 4 gives 5.0, not 5. Use / when you need the exact share, and // when you need only whole stickers each.
biscuits = 52
friends = 6
print(biscuits / friends)
print(biscuits - 70)
print(biscuits * friends)Output:
8.666666666666666
-18
312
Change either number and the results change, but division with / still gives a decimal answer while subtraction can still drop below zero.
Edexcel GCSE Computer Science · Arithmetic Operators
Edexcel GCSE Computer Science · Arithmetic Operators