Solved
Real use for modulo besides even/odd?
OP
Every tutorial shows n % 2 == 0. What else is % actually used for?
Every tutorial shows n % 2 == 0. What else is % actually used for?
Comments 4
Log in to join the discussion.
Log in Create accountLoads of things:
- every Nth item:
if i % 10 == 0: print("progress")- wrapping around:
day = (day + 1) % 7- splitting time:
minutes, seconds = divmod(total, 60)(divmod gives // and % at once)divmod! That's exactly what I needed for my countdown timer.
Clock arithmetic in general. Hours:
(23 + 5) % 24is 4.Also checking divisibility, like whether a year might be a leap year:
year % 4 == 0(plus the 100/400 rules).