Arithmetic Operators
+, -, *, /, //, %, and **.
4 minute read
Big idea
Python has seven arithmetic operators. Four you know from school; three — //, %, and ** — do the jobs that come up constantly in programs.
The operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3.5 |
// | floor divide (whole part) | 7 // 2 | 3 |
% | remainder (modulo) | 7 % 2 | 1 |
** | power | 7 ** 2 | 49 |
a = 17
b = 5
print(a + b, a - b, a * b)
print(a / b)
print(a // b, a % b)
print(a ** 2)output
22 12 85
3.4
3 2
289Run and edit it in the app.
// and % are a team
17 // 5 is how many whole 5s fit in 17 (3). 17 % 5 is what's left over (2). Together they split a number into "full groups" and "the rest":
total = 200
minutes = total // 60
seconds = total % 60
print(minutes, "min", seconds, "sec")output
3 min 20 secRun and edit it in the app.
% has a famous second use: checking divisibility. n % 2 is 0 exactly when n is even.
print(10 % 2)
print(11 % 2)
print(15 % 4)
print(4 % 15)What does it print? Show the answer
0
1
3
4Run and edit it in the app.
4 % 15 is 4: fifteen doesn't fit into 4 at all, so all 4 is left over.
Watch it
Powers
** raises to a power. 2 ** 10 is 1024. A fractional power gives roots: 9 ** 0.5 is 3.0, the square root.
side = 6
area = side ^ 2
print(area)output
36Run and edit it in the app.
In Python ^ is not power — it's a bitwise operator you won't need for a long time. Use **.
What is 23 // 4 and 23 % 4?
Think of it like
Sharing 17 sweets among 5 friends. 17 // 5 is how many each friend gets (3). 17 % 5 is how many are left in the bag (2).
Key terms
- Operator — A symbol that does something to values, like
+. - Floor division (
//) — Division that keeps only the whole part. - Modulo (
%) — The remainder after division.
Check yourself
What is 17 % 5?
Which operator raises to a power?
How can you tell if n is even?