Python Numbers and Math: Operators, //, %, and Precedence

Python 3.14 · ✓ verified by execution on 2026-07-19

Python is a capable calculator. The four operators you’d expect all work — run this and change the numbers:

python
print(7 + 3)
print(10 - 4)
print(6 * 5)
Output
10
6
30

Division has a twist you met earlier: / always gives a float, even for a clean division.

python
print(10 / 2)
Output
5.0

Two special divisions: // and %

Often you want the whole number part of a division, or the leftover. Python has an operator for each.

Floor division // divides and throws away the fraction, giving a whole number:

python
print(17 // 5)
Output
3

Modulo % gives the remainder — what’s left over:

python
print(17 % 5)
Output
2

Read those together: 5 goes into 17 three times (that’s 17 // 5), with 2 left over (that’s 17 % 5). This pair shows up constantly — splitting things into groups, checking if a number is even (n % 2), wrapping around a clock, and much more.

Powers with **

Use ** to raise a number to a power. (Not ^ — that means something else in Python.)

python
print(2 ** 10)
Output
1024

Order of operations

Python follows the maths rules you already know: ** first, then * / // %, then + -. So multiplication happens before addition:

python
print(2 + 3 * 4)
Output
14

Use parentheses when you want a different order — they always win:

python
print((2 + 3) * 4)
Output
20

Building a running total

Remember reassignment from the last lesson? Combine it with math and you get an accumulator — a variable you keep adding to. The right-hand side is worked out first, then stored back into the name. Press Visualize and watch total climb:

python · visualize
total = 0
total = total + 100
total = total + 250
total = total + 50
print(total)

Each line takes the current total, adds to it, and stores the result back — so total grows 0 → 100 → 350 → 400. You’ll use this pattern in almost every program you write.

One error to know: dividing by zero

You can’t divide by zero — Python stops with a clear error:

python
print(10 / 0)
This example raises an error (on purpose)
ZeroDivisionError

Whenever a divisor might be zero, check it first. (% and // by zero raise the same error.)

Edge cases worth remembering

Check yourself

What is 5 / 2, and what is 5 // 2?

Reveal answer

5 / 2 is 2.5 (a float); 5 // 2 is 2 (floor division drops the fraction) — / always gives a float, so 5 / 2 is 2.5. // is floor division — it divides and rounds down to a whole number, giving 2.

What does 17 % 5 give?

Reveal answer

2 — the remainder after dividing 17 by 5 — % is the modulo (remainder) operator. 5 goes into 17 three times (15), leaving a remainder of 2.

What does 2 + 3 * 4 evaluate to?

Reveal answer

14 — multiplication happens before addition — Python follows math precedence: * before +. So 3 * 4 = 12 first, then 2 + 12 = 14. Parentheses change the order.

How do you calculate 2 to the power of 8 in Python?

Reveal answer

2 ** 8 — ** is the exponent operator, so 2 ** 8 is 256. ^ exists but means something else (bitwise XOR), and 2 * 8 is just 16.

Challenges

Challenge 1 +10 XP

Print the remainder when 23 is divided by 4.

python
  • Test 1 — expects "3"
Need a hint? (−25% XP)

The remainder operator is %.

Show solution (0 XP)
print(23 % 4)

Challenge 2 +15 XP

A shop sells pens in packs of 6. You have 20 pens. On the first line print how many full packs you can make; on the second line print how many pens are left over.

python
  • Test 1 — expects "3\n2"
Need a hint? (−25% XP)

Use // for how many full packs fit, and % for what's left over.

Show solution (0 XP)
print(20 // 6)
print(20 % 6)