Seven operators
Arithmetic is the part of Python you will use in literally every program, and the three unfamiliar operators at the bottom of this table are the ones working code leans on: // and % split totals into units (seconds into minutes, items into pages), and % powers every is-it-divisible and wrap-around check you will ever write.
Python has seven arithmetic operators. The first four are familiar, the last three do the heavy lifting in real programs:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 3 | 10 |
- | subtract | 7 - 3 | 4 |
* | multiply | 7 * 3 | 21 |
/ | divide | 7 / 2 | 3.5 |
// | floor divide (drop the remainder) | 7 // 2 | 3 |
% | modulo (the remainder) | 7 % 2 | 1 |
** | power | 2 ** 10 | 1024 |
Two things to burn in now. First, / always produces a float, even 10 / 5 gives 2.0. Second, // and % come as a pair: // answers how many whole times does it fit and % answers what is left over. n % 2 == 0 is the classic even-number test.
The seven operators side by side
Each line below corresponds to a row of the table above, and the pair worth studying hardest is / against //.
print(7 / 2) print(7 // 2) print(7 % 2) print(10 / 5) print(2 ** 10)
Output
3.5 3 1 2.0 1024
The first three lines all divide 7 by 2 but report different things: the exact answer 3.5, the whole-number part 3, and the remainder 1. The fourth line shows that / produces a float even when the division comes out even, which is why 10 / 5 prints 2.0 rather than 2.
Order of operations
Python follows math class: ** first, then * / // %, then + -. Operators at the same level run left to right.
8 + 2 * 3 # 14, not 30 (8 + 2) * 3 # 30, parentheses win
When an expression is not instantly obvious, add parentheses. They cost nothing and make the intent visible.
17 // 5 evaluates to 3.
Floor division reports how many whole times the divisor fits and discards what is left over. Five fits into seventeen three complete times, so the result is 3, and it is an int rather than 3.0. The discarded part is not lost from the language, just from this operator: 17 % 5 picks it up and gives 2.
Splitting a duration into larger units is the everyday job of // and % working as a pair. For a 200-minute film, // counts the whole hours and % keeps whatever minutes are left over.
total = 200 hours = total // 60 minutes = total % 60 print(hours, "h", minutes, "min")
Output
3 h 20 min
The two operators divide the work between them: total // 60 finds that three full hours fit into 200 minutes, and total % 60 reports the 20 minutes that do not make up a fourth hour. The single print then interleaves numbers and labels, with Python adding a space at each comma.
Wrapping around with modulo
The second big job of % is handling values that cycle. Clock hours, days of the week, and positions around a circle all wrap back to the start, and taking the remainder is what performs that wrap. Nine o'clock plus five hours is (9 + 5) % 12, and Saturday as day 6 plus three days is (6 + 3) % 7.
start = 9 hours_later = 5 print((start + hours_later) % 12) day = 6 print((day + 3) % 7)
Output
2 2
Both lines add first and then take the remainder, which is what the parentheses enforce. In the clock case the sum 14 exceeds 12, so the remainder brings it back to 2 o'clock, and in the calendar case the sum 9 exceeds 7, giving day 2, which is Tuesday. The divisor is always the length of the cycle, 12 for clock hours and 7 for days.
Evaluating 8 + 2 * 3 ** 2 the way Python does gives 26.
The precedence rules settle the order. Power binds tightest, so 3 ** 2 becomes 9. Multiplication comes next, turning 2 * 9 into 18. Addition runs last, and 8 + 18 gives 26.
Reaching a different answer would require parentheses to override that order. Writing (8 + 2) * 3 ** 2 would add first and produce 90 instead, which is a good reason to add parentheses whenever the intended grouping is not immediately obvious to a reader.