Programming Fundamentals
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.
Arithmetic operators work on values and give one result. Addition uses the + operator and combines two values into a single total. Subtraction uses - and finds the difference between two values. Multiplication uses * and finds the product of two values.
| Operator | Name | What it does | Example |
|---|---|---|---|
+ | Addition | Combines two values into one total | 46 + 37 gives 83 |
- | Subtraction | Finds the difference between two values | 46 - 37 gives 9 |
* | Multiplication | Finds the product of two values | 6 * 14 gives 84 |
In Python you write the operator between the two values it works on, and it produces one result. The order of the values does not matter for addition or multiplication, but it does for subtraction: 46 - 37 gives 9 while 37 - 46 gives -9. A negative answer shows that the second value is the larger of the two.
The + operator does different jobs depending on the type of the values either side of it. With two numbers it adds them, but with two strings it joins them end to end into one longer string. So "Good " + "morning" gives "Good morning". A space counts as a character too, so without it "Good" + "morning" gives "Goodmorning"; to keep the gap between the words you must put the space inside one of the strings.
points_home = 34
points_away = 51
print(points_home + points_away) # total points scored
print(points_away - points_home) # winning margin
trays = 12
cakes_per_tray = 8
print(trays * cakes_per_tray) # 12 trays of 8 cakes
print("Good " + "morning")Output:
85
17
96
Good morningDivision means splitting a value into equal parts. In Python you use the forward slash / between two values. The result is the full answer, including any fractional part, so nothing is rounded away.
For example, if a 3-litre bottle of juice is poured equally into 8 glasses, each glass gets 3 / 8, which is 0.375 litres. The fractional part matters: a whole-number answer would not show the exact amount in each glass.
When a division works out exactly, Python still gives a real number. For example, 55 / 11 gives 5.0, not 5. The / operator always returns a real result, whatever the two values are.
distance_km = 21
time_hours = 4
print(distance_km / time_hours)
print(18 / 6)Output:
5.25
3.0
Division with / is not the same as //, which gives only the whole-number part, or %, which gives the remainder. For the full answer to a division, always use /.
When you divide one whole number by another you can pick out two useful pieces of information: how many whole times the second number fits, and what is left over. Python has an operator for each of them.
The quotient, also called integer division, uses // and gives the whole number of times one number divides into another. With positive whole numbers the fractional part is discarded rather than rounded, so 38 // 4 gives 9: 4 fits into 38 nine whole times (4 × 9 = 36) and there is not enough left to make a tenth group.
Modulo uses % and gives the remainder left behind by that whole-number division, so 38 % 4 gives 2. When the first number divides exactly by the second, nothing is left over and modulo gives zero, so 48 % 6 gives 0.
| Operator | What it gives | 74 books packed in boxes of 8 |
|---|---|---|
/ | the full answer, including any fractional part | 9.25 |
// | the whole-number part only | 9 |
% | the remainder | 2 |
You can always rebuild the number you started with (the dividend): the quotient multiplied by the number you divided by (the divisor), plus the remainder, equals the dividend. With 74 and 8 that is 9 × 8 = 72 and 72 + 2 = 74, which in Python is written (74 // 8) * 8 + (74 % 8). This check also reminds you that // is not rounding to the nearest whole number — it keeps only the whole-number part.
If the divisor is larger than the dividend, no complete group can be made, so the quotient is 0 and the remainder is the dividend itself: 5 // 8 gives 0 and 5 % 8 gives 5.
Integer division and modulo are used with whole numbers, and exam questions use positive whole numbers so the results stay predictable.
This program works out both parts of a division from two values you type in.
dividend = int(input("Number to divide: "))
divisor = int(input("Divide by: "))
quotient = dividend // divisor # whole-number part
remainder = dividend % divisor # what is left over
print("Quotient:", quotient)
print("Remainder:", remainder)Output when the user types 94, 8:
Number to divide: 94
Divide by: 8
Quotient: 11
Remainder: 6Exponentiation means raising a number to a power: a number is multiplied by itself again and again. In Python you do it with the power operator **, written between the base (the number being multiplied) and the exponent (how many copies of the base are multiplied together).
So 4 ** 2 gives 16, because 4 × 4 = 16, and 3 ** 3 gives 27, because 3 × 3 × 3 = 27. The value on the left is the number you start with; the value on the right tells you how many of them to multiply.
9 ** 2 gives 81.4 ** 3 gives 64.| Operation | Result | Why |
|---|---|---|
6 ** 2 | 36 | 6 × 6 |
6 ** 3 | 216 | 6 × 6 × 6 |
10 ** 4 | 10000 | 10 × 10 × 10 × 10 |
Exponentiation is useful whenever the same value is multiplied over and over. A four-digit PIN chosen from the digits 0 to 9 has 10 000 possible codes, because each of the four positions can be any of ten digits: 10 ** 4 = 10 × 10 × 10 × 10.
The power operator is ** and not ^. The ^ symbol is a different operator that does a different job, so 6 ^ 2 does not mean “six squared” in Python and will not give you 36. Always type two asterisks side by side.
hours = int(input("How many hours has the colony grown for? "))
print(2 ** hours) # bacteria after that many hoursOutput when the user types 6:
How many hours has the colony grown for? 6
64
The exponent does not have to be typed as a number. A colony of bacteria that doubles every hour has 2 ** hours bacteria, so the program raises 2 to whatever value the user enters, and the answer grows very quickly.
When you do arithmetic in Python, the answer is not always a whole number. Every result has a type: either an integer (a whole number) or a real (a number with a decimal point, called a float in Python). Which one you get depends on the operator you use and on whether the values you start with are integers or reals.
The / operator always returns a real, even when the division works out exactly. 45 / 9 gives 5.0, not 5. Python writes the .0 to show the answer is a real, because a real is allowed to hold a fractional part even when this particular answer has not got one.
If both values are positive integers, then +, -, *, //, % and ** all give an integer result. The table shows some expressions and the type of each result, so you can see how the type changes.
| Expression | Result | Type of result | Why |
|---|---|---|---|
| 25 + 4 | 29 | integer | both values are integers |
| 25 // 4 | 6 | integer | both values are integers |
| 4 ** 3 | 64 | integer | both values are integers |
| 25 / 4 | 6.25 | real | / always gives a real |
| 45 / 9 | 5.0 | real | / always gives a real, even when it divides exactly |
| 25 + 1.5 | 26.5 | real | one of the values is a real |
| 16 * 0.5 | 8.0 | real | one of the values is a real |
If either value in an arithmetic operation is a real, the result is a real too, whichever operator you use. So 25 + 1.5 gives 26.5, and 16 * 0.5 gives 8.0 — a whole-number value, but still a real because a real value was involved. When both values are positive integers, only / gives a real by itself; with the other operators you need at least one real value in the calculation before the answer becomes a real.