Solved

Real use for modulo besides even/odd?

EmmaCodesOP

Every tutorial shows n % 2 == 0. What else is % actually used for?

11

Comments 4

Log in to join the discussion.

Log in Create account
  1. FreyaL Solutionreplies: 1

    Loads 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)

    9
    1. EmmaCodesOP

      divmod! That's exactly what I needed for my countdown timer.

      2
    2. MateoG

      Clock arithmetic in general. Hours: (23 + 5) % 24 is 4.

      3
      1. ElaKowalska

        Also checking divisibility, like whether a year might be a leap year: year % 4 == 0 (plus the 100/400 rules).

        -1