Lesson 7: All the arithmetic operators
Seven operators do all the arithmetic in Python: add, subtract, multiply, divide, floor divide, remainder and power. One short program shows each one, plus the order Python does them in.
In short
- Python has seven arithmetic operators: + - * / // % **.
- `/` always gives a float, even for 8 / 2. `//` gives a whole number and throws the decimal part away.
- `%` gives the remainder, which is the basis of the even/odd and prime checks later in this series.
- Multiplication and division happen before addition and subtraction; brackets change that.
7
arithmetic operators in Python
Python docs
17 ** 5
1419857 — power is a single operator
this program
What this program shows
An operator is the symbol between two values that tells Python what to do with them. The values on either side are called operands. Arithmetic operators work on numbers, and the answer is a number.
The seven operators
| Operator | Name | Example | Answer |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | Division | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Remainder (modulo) | 7 % 2 | 1 |
** | Power | 7 ** 2 | 49 |
The program
# arithmetic.py — every arithmetic operator, with its answer in a comment
a = 17
b = 5
print("a + b =", a + b) # 22
print("a - b =", a - b) # 12
print("a * b =", a * b) # 85
print("a / b =", a / b) # 3.4 division gives a float
print("a // b =", a // b) # 3 floor division drops the decimal part
print("a % b =", a % b) # 2 what is left over
print("a ** b =", a ** b) # 1419857
print(2 + 3 * 4) # 14, because * runs before +
print((2 + 3) * 4) # 20, brackets first
print(7 / 2) # 3.5
print(-7 // 2) # -4, floor goes down, not towards zero
How it works, step by step
a = 17andb = 5give us two numbers to work with.- The first four prints add, subtract, multiply and divide. Notice that
a / bprints3.4— the slash always returns a float. a // basks how many whole times 5 fits into 17. It does not round; it cuts the decimal part off.a % basks what remains after those whole 5s: 17 = 5 + 5 + 5 + 2, so the answer is 2.a ** bmeans 17 × 17 × 17 × 17 × 17.- The last four prints show the order. Multiplication happens first, so
2 + 3 * 4is2 + 12. Brackets change the order, which is why(2 + 3) * 4is 20.
What it prints
a + b = 22 a - b = 12 a * b = 85 a / b = 3.4 a // b = 3 a % b = 2 a ** b = 1419857 14 20 3.5 -4
Common mistakes
- Expecting
7 / 2to give 3. It gives 3.5. Use//when you want the whole part. - Thinking
%means percentage. It means remainder. - Forgetting that
+also joins text."2" + "3"gives"23", not 5. - Assuming
//rounds to the nearest number. It always goes down:-7 // 2is-4.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Take two numbers, 9 and 4, and print their sum, difference, product and quotient.
- Print the remainder when 100 is divided by 7, then explain the answer in a comment.
- Use `**` to print 2 to the power 10.
- Work out the average of three numbers 45, 67 and 89 using `/` and brackets.
- Predict the answer to `10 + 2 * 3 ** 2` before you run it, then check.
Sources
Frequently asked questions
What is the difference between / and // in Python?
/ divides and always returns a float, so 7 / 2 is 3.5. // divides and returns the whole part, so 7 // 2 is 3. Use // when you want a count of whole items.
What does the % operator do?
It gives the remainder after a division. 17 % 5 is 2 because 5 fits into 17 three times with 2 left over. It is used for even/odd tests, prime tests and wrapping numbers into a range.
Which operator runs first in Python?
Power (**), then multiplication, division, floor division and remainder, then addition and subtraction. Comparisons come after that. Brackets override everything, so use them when you are unsure.
Can I use arithmetic operators on text?
+ joins two strings and * repeats a string, as in "ab" * 2 giving "abab". Every other arithmetic operator only works on numbers.