22 min read
Arithmetic Operators
+, −, *, /, // (whole-number division), % (remainder) and ** (power), and the order Python works them out.
The Arithmetic Operators
| Arithmetic operators | |||
|---|---|---|---|
Try every operatorPython
print(17 + 5)print(17 - 5)print(17 * 5)print(17 / 5)print(17 // 5)print(17 % 5)print(2 ** 3)Expected output
22 12 85 3.4 3 2 8
// and % Together
Converting minutesPython
minutes = 135hours = minutes // 60left_over = minutes % 60print(hours, "hours and", left_over, "minutes")Expected output
2 hours and 15 minutes
% also works with decimals in PythonPython
print(7.5 % 2)print(10 % 2.5)Expected output
1.5 0.0
Order of Operations (Precedence)
| Python works out operators in this order | |
|---|---|
Check the precedence examplesPython
print(2 + 3 * 4)print((2 + 3) * 4)print(10 - 4 / 2)print(2 ** 2 * 3)Expected output
14 20 8.0 12
Practice
What is 29 % 6?
More lessons in Python Programming · Next: Comparison, Logical and Assignment Operators · Previous: Data Types
