PyDA Course
📖 Lesson 5 Beginner ⏱ 12 min ⚡ +10 XP ColabKagglenbviewerBinderDeepnoteGitHub arithmetic · division · modulo · exponent · precedence

Arithmetic Operators

Add, subtract, multiply, divide, floor-divide, modulo, and exponent, all eight arithmetic operators.

🎯 What you'll learn:
  • 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:

python
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:

python
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: 72=497^2 = 49. 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?

15=43+3.15 = 4 \cdot 3 + 3.

The answer has two parts, the quotient 33 and the remainder 33. Python’s // answers the first part and % answers the second:

a=(a//b)b+(a%b)a = (a \mathbin{//} b) \cdot b + (a \mathbin{\%} b)
python
15 // 4   # 3   — how many groups of 4
15 % 4    # 3   — what's left over
4 * 3 + 3 # 15  ✓ the identity holds

That 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 7÷2-7 \div 2? Write it as a grouping question:

7=2?+?.-7 = 2 \cdot ? + ?.

The options are 2(3)+(1)2 \cdot (-3) + (-1) or 2(4)+12 \cdot (-4) + 1. Python floors, like the mathematical function x\lfloor x \rfloor:

python
-7 // 2   # -4 — floor(-3.5) = -4, not -3
-7 % 2    # 1  — consistent with the floor: -7 = 2·(-4) + 1

The two operators stay honest to each other, the identity a=(a//b)b+(a%b)a = (a//b)\cdot b + (a\%b) 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 2+342 + 3 \cdot 4. Python adopts the order you learned as PEMDAS:

  • ** first (exponentiation)
  • then *, /, //, % (left to right)
  • then +, - (left to right)
python
2 + 3 * 4      # 14, not 20
(2 + 3) * 4    # 20 — parentheses override
2 ** 3 ** 2    # 512, not 64

That last one is a genuine surprise. ** is right-associative, so 2 ** 3 ** 2 reads as 2(32)=29=5122^{(3^2)} = 2^9 = 512, 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:

python
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 holds

The division identity a=(a//b)b+(a%b)a = (a \mathbin{//} b) \cdot b + (a \mathbin{\%} b) 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 / 2 is 3.5 (a float); 7 // 2 is 3 (an int). Reach for // only when the whole-quotient is what the problem needs.
  • Floor division with negatives. -7 // 2 is -4, not -3. The floor goes toward -\infty, not toward zero.
  • % works on floats too. 7.5 % 2 is 1.5, the identity above holds for reals as well as integers.
  • ** binds tighter than *. 2 * 3 ** 2 is 18, not 36, 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 4(15//4)+(15%4)4 \cdot (15 // 4) + (15 \% 4) reproduces 1515.

💡 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 ** 2 is 512, not 64. 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?

arithmeticdivisionmoduloexponentprecedence