Challenge 1 +10 XP
Print the remainder when 23 is divided by 4.
- Test 1 — expects "3"
Need a hint? (−25% XP)
The remainder operator is %.
Show solution (0 XP)
print(23 % 4) Python is a capable calculator. The four operators you’d expect all work — run this and change the numbers:
print(7 + 3)
print(10 - 4)
print(6 * 5)10 6 30
Division has a twist you met earlier: / always gives a float, even for a clean
division.
print(10 / 2)5.0
// 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:
print(17 // 5)3
Modulo % gives the remainder — what’s left over:
print(17 % 5)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.
**Use ** to raise a number to a power. (Not ^ — that means something else in Python.)
print(2 ** 10)1024
Python follows the maths rules you already know: ** first, then * / // %, then
+ -. So multiplication happens before addition:
print(2 + 3 * 4)14
Use parentheses when you want a different order — they always win:
print((2 + 3) * 4)20
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:
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.
You can’t divide by zero — Python stops with a clear error:
print(10 / 0)ZeroDivisionError
Whenever a divisor might be zero, check it first. (% and // by zero raise the same
error.)
ZeroDivisionError — guard against a
zero divisor.-7 // 2 is -4, not -3 — a
surprise once negative numbers are involved.What is 5 / 2, and what is 5 // 2?
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?
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?
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?
2 ** 8 — ** is the exponent operator, so 2 ** 8 is 256. ^ exists but means something else (bitwise XOR), and 2 * 8 is just 16.
Print the remainder when 23 is divided by 4.
The remainder operator is %.
print(23 % 4) 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.
Use // for how many full packs fit, and % for what's left over.
print(20 // 6)
print(20 % 6)