Sub topic 6
Arithmetic operators and precedence
This chapter assumes you can run Python code, use print(), and know the difference between an int and a float. It covers the arithmetic operators, how Python decides which one runs first, and the mistakes that most often trip people up.
The operators
Python has seven arithmetic operators.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `+` | addition | `7 + 2` | `9` |
| `-` | subtraction | `7 - 2` | `5` |
| `*` | multiplication | `7 * 2` | `14` |
| `/` | true division | `7 / 2` | `3.5` |
| `//` | floor division | `7 // 2` | `3` |
| `%` | modulo (remainder) | `7 % 2` | `1` |
| `**` | exponentiation | `2 ** 10` | `1024` |
The result type depends on the operands. / always returns a float, even for 6 / 3, which gives 2.0. The other operators return an int when both operands are int, and a float if either is a float.
print(7 / 2)
print(7 // 2)
print(-7 // 2)
print(-7 % 2)
print(2 ** 10)This prints 3.5, 3, -4, 1, and 1024.
Floor division and modulo with negatives
// rounds down toward negative infinity. It does not truncate toward zero. That is why -7 // 2 is -4 and not -3.
% is defined to match. For integers, a == (a // b) * b + a % b always holds. With -7 and 2, the quotient is -4, so the remainder is -7 - (-4 * 2), which is 1. The result of % takes the sign of the right operand.
a, b = -7, 2
print((a // b) * b + a % b == a)
print(divmod(a, b))This prints True and then (-4, 1). The built-in divmod() returns the floor quotient and the remainder together.
Precedence
When an expression has several operators, Python applies them in a fixed order. From highest to lowest priority:
- Parentheses
( ) - Exponentiation
** - Unary signs
+x,-x *,/,//,%+,-
Operators at the same level run left to right, with one exception: ** groups right to left. So 2 ** 3 ** 2 means 2 ** (3 ** 2), which is 512.
A common surprise involves the unary minus. Because ** ranks above it, -2 ** 2 means -(2 ** 2), which is -4.
print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 ** 2)
print(-2 ** 2)
print((-2) ** 2)
print(10 - 4 - 3)This prints 14, 20, 512, -4, 4, and 3. The last line shows left-to-right grouping: (10 - 4) - 3.
Common errors
- Dividing by zero.
1 / 0,1 // 0, and1 % 0all raiseZeroDivisionError. - Expecting
/to return anint. Use//when you need a whole-number quotient. - Trusting negative
//and%. Results differ from languages such as C, which truncate toward zero. - Relying on precedence you have not memorized. Add parentheses. They cost nothing and make the intent clear.
try:
print(1 / 0)
except ZeroDivisionError as e:
print("ZeroDivisionError:", e)This prints ZeroDivisionError: division by zero.
Exercises
Exercise
What do print(-9 // 4) and print(-9 % 4) output, and which identity connects the two results with -9 and 4?
Show answer
They print -3 and 3. Floor division rounds down, so -9 // 4 is -3. The remainder is -9 - (-3 * 4), which is 3. The identity is (-9 // 4) * 4 + (-9 % 4) == -9.
Exercise
What is the value of 2 * 3 ** 2 - 4 / 2, and what is its type?
Show answer
The value is 16.0. First 3 ** 2 is 9, then 2 * 9 is 18, then 4 / 2 is 2.0, and 18 - 2.0 is 16.0. The type is float because / always returns a float.