Python Operators
What you will learn
- The seven arithmetic operators
- What
//(floor division) and%(remainder) really do, including with negative numbers - The order of operations (precedence) and when to use brackets
- Augmented assignment:
+=,-=,*=and the others - What happens when you divide by zero
1. The arithmetic operators
An operator is a symbol that does a calculation. Python has seven for numbers:
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
addition | 7 + 2 |
9 |
- |
subtraction | 7 - 2 |
5 |
* |
multiplication | 7 * 2 |
14 |
/ |
division | 7 / 2 |
3.5 |
// |
floor division | 7 // 2 |
3 |
% |
remainder (modulo) | 7 % 2 |
1 |
** |
power | 7 ** 2 |
49 |
print(7 + 2, 7 - 2, 7 * 2, 7 / 2, 7 // 2, 7 % 2, 7 ** 2)
9 5 14 3.5 3 1 49
The values the operator works on are called operands. In 7 + 2, the operands are 7 and 2.
Quick check: what does `10 / 4` give, and what does `10 // 4` give?
2.5 and 2. / gives the exact answer; // gives the whole-number part (rounded down).
2. Floor division and remainder
// and % work as a pair: how many whole times a number fits, and what is left over.
17 // 5 is 3 (5 fits 3 times) and 17 % 5 is 2 (the leftover):
| Expression | Meaning | Result |
|---|---|---|
17 // 5 |
how many whole fives in 17 | 3 |
17 % 5 |
what remains after taking out those fives | 2 |
They are connected by this rule: (a // b) * b + a % b == a. Check: 3 * 5 + 2 = 17.
Useful uses of %
- Even or odd:
n % 2is0for even numbers and1for odd. - Every n-th item:
i % 3 == 0is true for every third value. - Wrapping around:
(hour + 5) % 24keeps a clock hour between 0 and 23.
Negative numbers
// always rounds down (towards minus infinity), not towards zero. And the result of % takes the sign of the second number:
print(-7 // 2, -7 % 2, 7 // -2, 7 % -2)
-4 1 -4 -1
-7 // 2is-4, not-3, because-3.5rounded down is-4.- The rule still holds:
(-7 // 2) * 2 + (-7 % 2)is-8 + 1, which is-7.
With floats, the same operators work: 7.5 // 2 is 3.0 and 7.5 % 2 is 1.5.
3. The power operator
** raises to a power. A negative power gives a fraction, and a power of 0.5 is a square root:
print(2 ** -1, 2 ** 0.5, 4 ** 0.5)
0.5 1.4142135623730951 2.0
The result of 2 ** -1 is a float, because the answer is not a whole number (lesson 3.1).
4. The order of operations
When an expression has several operators, Python does them in a fixed order, from highest to lowest:
| Priority | Operators |
|---|---|
| 1 (first) | brackets ( ) |
| 2 | ** |
| 3 | unary + and - (as in -5) |
| 4 | *, /, //, % |
| 5 (last) | +, - |
Operators on the same level go left to right, except **, which goes right to left.
print(2 + 3 * 4 ** 2)
print((2 + 3) * 4)
print(100 / 10 / 5)
print(2 ** 3 ** 2)
print(-2 ** 2, (-2) ** 2)
50
20
2.0
512
-4 4
| Expression | How Python reads it | Result |
|---|---|---|
2 + 3 * 4 ** 2 |
2 + (3 * (4 ** 2)) = 2 + 48 |
50 |
100 / 10 / 5 |
(100 / 10) / 5 |
2.0 |
2 ** 3 ** 2 |
2 ** (3 ** 2) = 2 ** 9 |
512 |
-2 ** 2 |
-(2 ** 2), because ** comes first |
-4 |
(-2) ** 2 |
the brackets make -2 the base |
4 |
When in doubt, add brackets. They cost nothing and make the code easier to read.
5. Augmented assignment
When you update a name using its own value, there is a shortcut:
| Long form | Short form |
|---|---|
x = x + 5 |
x += 5 |
x = x - 3 |
x -= 3 |
x = x * 2 |
x *= 2 |
x = x / 4 |
x /= 4 |
x = x // 2 |
x //= 2 |
x = x ** 3 |
x **= 3 |
x = x % 5 |
x %= 5 |
x = 10
x += 5
print(x)
x -= 3
print(x)
x *= 2
print(x)
x /= 4
print(x)
15
12
24
6.0
Notice that x /= 4 turned x into a float, because / always gives a float. For numbers, x += 1 makes a new number and moves x to it (lesson 2.6).
6. Dividing by zero
Dividing by zero is impossible, and Python stops with a ZeroDivisionError. The message depends on the operator:
| Expression | Error message |
|---|---|
1 / 0 |
division by zero |
1 // 0 |
integer division or modulo by zero |
1 % 0 |
integer modulo by zero |
1.0 / 0 |
float division by zero |
To avoid it, check the divisor before dividing (chapter 5) or catch the error (chapter 14).
Worked example: hours and minutes from minutes
Convert 400 minutes into hours and minutes.
total = 400
hours = total // 60
minutes = total % 60
print(hours, minutes)
6 40
| Step | Calculation | Meaning |
|---|---|---|
| 1 | 400 // 60 is 6 |
6 whole hours |
| 2 | 400 % 60 is 40 |
40 minutes left over |
Check: 6 * 60 + 40 is 400.
Common mistakes
- Expecting
//to round towards zero.-7 // 2is-4, not-3. - Writing
-2 ** 2and expecting4. The power is done first, so the answer is-4; use(-2) ** 2. - Using
^for a power.^is a different operator (lesson 3.7). Use**. - Forgetting that
/gives a float.6 / 3is2.0, not2; use//for a whole number. - Dividing by a value that might be zero without checking it.
Key takeaways
- The operators are
+ - * / // % **. //gives the whole-number part (rounded down) and%the remainder; together they satisfy(a // b) * b + a % b == a.- The order is brackets,
**, unary minus, then* / // %, then+ -. Use brackets when unsure. x += 1is shorthand forx = x + 1; the same works for the other operators.- Dividing by zero raises
ZeroDivisionError.
Practice
1. Calculate by hand, then check: 3 + 4 * 2 and (3 + 4) * 2.
Show answer
11 and 14. Multiplication goes before addition unless brackets say otherwise.
2. What do 29 // 7 and 29 % 7 give?
Show answer
4 and 1, because 4 * 7 + 1 is 29.
3. Write an expression that is true for even numbers, using %.
Show answer
n % 2 == 0.
4. What is -9 // 2? And -9 % 2?
Show answer
-5 and 1. Floor division rounds down (-4.5 goes to -5), and -5 * 2 + 1 is -9.
5. What does 2 ** 3 ** 2 give, and why not 64?
Show answer
512. ** goes right to left, so it is 2 ** (3 ** 2), which is 2 ** 9. Calculating left to right would give (2 ** 3) ** 2 = 64.
6. Convert 5000 seconds into minutes and leftover seconds with one line each.
Show answer
minutes = 5000 // 60 gives 83, and seconds = 5000 % 60 gives 20. Check: 83 * 60 + 20 is 5000.