Doing maths is a process: it happens inside the computer, between the input and the output. Python does arithmetic with operators, and several of them are written differently from the way you would write them on paper.
| Operator | What it does | Example | Result |
|---|---|---|---|
+ |
addition | 8 + 2 |
10 |
- |
subtraction | 8 - 2 |
6 |
* |
multiplication | 8 * 2 |
16 |
/ |
division, with a decimal result | 10 / 4 |
2.5 |
// |
division, whole number part only | 10 // 4 |
2 |
% |
modulo: the remainder after division | 10 % 4 |
2 |
** |
exponentiation: raising to a power | 8 ** 2 |
64 |
You have already met + joining two strings together. Between two numbers the
same symbol adds them up instead, so what + does depends entirely on what sits
on either side of it.
Python has two division operators, and they answer two different questions.
/ is ordinary division. It gives the full answer, including everything after
the decimal point. A number with a decimal point in it is called a float:
print(10 / 4)
2.5
// is floor division, also called integer division. It throws away
everything after the decimal point and keeps only the whole number part. A whole
number is called an integer, or int for short:
print(10 // 4)
2
Notice that // does not round to the nearest whole number. 10 // 4 is 2, even
though 2.5 is exactly halfway: the decimal part is simply dropped.
/ always produces a float, even when the division comes out exactly.
print(8 / 2) outputs 4.0, not 4. If you want the answer as a plain whole
number, // is the operator you want.
% is called modulo. It divides, like //, but gives you the part that is
left over instead of the number of whole times. 4 goes into 10 twice, which
uses up 8, so the remainder is 2:
print(10 % 4)
2
The % sign has nothing to do with percentages here: in Python it always means
the remainder.
** is called exponentiation. It raises the first number to the power of
the second, so 8 ** 2 is 8 squared, 8 times 8, and 2 ** 3 is 2 cubed, 2
times 2 times 2:
print(8 ** 2)
print(2 ** 3)
64
8
One star multiplies, two stars raise to a power.
Numbers do not have to be typed straight into the calculation. If they are stored in variables, you can use the variable names instead, and Python looks inside each one before it calculates:
num1 = 5
num2 = 10
result = num1 + num2
print(result)
15
There are three steps there, and it is worth naming them:
Step 2 is the one that is easy to skip. A calculation on its own throws its answer away; it only survives if you assign it to something.
The variable you assign the result to may also be one of the variables used in the calculation. Python works out the right hand side first, and only then replaces the old value:
score = 0
score = score + 1
print(score)
1
Because adding to a variable’s own value is so common, Python has a shorthand
for it. score += 1 means exactly the same thing as score = score + 1, and
lives -= 1 means the same as lives = lives - 1:
score = 0
score += 1
print(score)
1
Run the code below. Then swap the operator on the result line for each of the
other six in turn, and check that every answer is the one you expected.
num1 = 20 num2 = 5 result = num1 * num2 print(result)