Arithmetic Operators
Add, subtract, multiply, divide, floor-divide, modulo, and exponent, all eight arithmetic operators.
- Use all eight arithmetic operators: +, -, *, /, //, %, **
- Understand floor division vs true division
- Apply operator precedence (PEMDAS)
- Use parentheses to override precedence
The operators a machine must steal from a mathematician
You have written helper functions in earlier lessons: store a value, print a value, change its type. None of that is useful until a program can do something to numbers. So pause and take stock: a computer exists to evaluate expressions, and every expression is built from operators joining values. You already know the arithmetic ones from paper, but the machine splits two of them in half.
The operators and their meanings
Python provides eight. The first four are exactly what you expect:
7 + 2 # 9 — addition
7 - 2 # 5 — subtraction
7 * 2 # 14 — multiplication
7 / 2 # 3.5 — true division (always returns a float)Then come three that answer questions you only ever asked in homework:
7 // 2 # 3 — floor division (rounds toward −∞)
7 % 2 # 1 — modulo (the remainder)
7 ** 2 # 49 — exponentiation (7²)** is Python’s writing of a power: . The two newcomers are // and %, and they are not variations, they are two halves of one legal question.
Two halves of one division
Ask a real question: how many whole groups of 4 fit into 15, and what is left over?
The answer has two parts, the quotient and the remainder . Python’s // answers the first part and % answers the second:
15 // 4 # 3 — how many groups of 4
15 % 4 # 3 — what's left over
4 * 3 + 3 # 15 ✓ the identity holdsThat identity is not decoration, it is the definition of both operators, and it cannot fail while the two parts are calculated by the same machine.
There is one wrinkle. Which quotient does Python report for ? Write it as a grouping question:
The options are or . Python floors, like the mathematical function :
-7 // 2 # -4 — floor(-3.5) = -4, not -3
-7 % 2 # 1 — consistent with the floor: -7 = 2·(-4) + 1The two operators stay honest to each other, the identity holds with no exceptions, and that is worth more than “the intuitive answer.”
The order of operations, settled
Expressions containing several operators need a fixed sequencing, or every reader would compute a different value for . Python adopts the order you learned as PEMDAS:
**first (exponentiation)- then
*,/,//,%(left to right) - then
+,-(left to right)
2 + 3 * 4 # 14, not 20
(2 + 3) * 4 # 20 — parentheses override
2 ** 3 ** 2 # 512, not 64That last one is a genuine surprise. ** is right-associative, so 2 ** 3 ** 2 reads as , matching the stacked notation where powers climb upward in one direction. When in doubt, spell parentheses out, a reader who does not see them will not guess your intent.
A worked example: change for the reading plan
The quotient/remainder pair runs a reading plan:
pages = 301
per_day = 30
days = pages // per_day # 10 — whole days of reading
leftover = pages % per_day # 1 — the 11th day's remnant
print(f"{days} full days, {leftover} leftover")
days * per_day + leftover # 301 — the identity holdsThe division identity becomes a ledger: days and leftover are its two columns, and the identity is the receipt that proves nothing was lost.
Common pitfalls
/vs//.7 / 2is3.5(a float);7 // 2is3(an int). Reach for//only when the whole-quotient is what the problem needs.- Floor division with negatives.
-7 // 2is-4, not-3. The floor goes toward , not toward zero. %works on floats too.7.5 % 2is1.5, the identity above holds for reals as well as integers.**binds tighter than*.2 * 3 ** 2is18, not36, the power is computed first. Parenthesize when you mean(2 * 3) ** 2= 36.
🧩 Challenges
🧩 Challenge, think first, then reveal
Without running it, compute 15 // 4 and 15 % 4 by hand, then verify that reproduces .
💡 Answer: 15 // 4 is 3 (the floor of $3.75$), and 15 % 4 is 3, since $15 = 4\cdot 3 + 3$. Together 4 * 3 + 3 = 15, the division identity, verified.
🧩 Challenge, think first, then reveal
How would you extract the hundreds digit of any number? Given n = 4567, get 5 using arithmetic only, no strings.
💡 Answer: (n // 100) % 10, first divide by 100 to shift the digit right (4567 → 45), then modulo 10 to keep only the last digit (45 → 5).
🧩 Challenge, think first, then reveal
Why does Python use ** for exponentiation instead of ^? What does ^ actually do in Python?
💡 Answer: ^ is the bitwise XOR operator in Python, not exponentiation. Python uses ** to avoid clashing with the convention of languages where ^ means XOR.
🤔 Socratic Questions
- Why does floor division round toward negative infinity rather than toward zero? What practical benefit falls out of that choice (hint: think of
divmod()returning a consistent pair)? 2 ** 3 ** 2is512, not64. Why is**right-associative when+and*are left-associative?- Where does modulo arithmetic earn its keep in real life? Think of clocks, calendar days, or array indices.
✅ Quick check
1. What is -7 // 2?
2. What is the result of 2 ** 3 ** 2?
3. What is 7 % 3?