How is the module useful in real life coding?

Hi there, hope everyone is doing safe during this pandemic;

I am taking the learn python: syntax course on Codecademy part of the -learn computer science path. I was wondering if someone could help me with the module as an operator, it is mentioned that it could be used to run something every nth time the code runs … could you please clarify how and why it works? also, what are other uses in real life coding for modules? excuse my ignorance.

thanks a lot.

Do you mean modulo operator? You seem to describe modulo operator, but you wrote module everywhere.

There are plenty of practical usages. You will run into them over time. We could use the modulo operator to determine of numbers are odd or even. Some people use it for styling (to have alternating colors)

When a concept is introduced, the practical applications might not always be clear, you will run into this more often. Just trust that over time the practical applications will come through. But you need to learn the concept first.

1 Like

Oh sorry its autocorrect. yes I meant the modulo operator. I just don’t get how they could be used to run something every nth time the code runs.

Well, if we have a loop, we could use the modulo operator to something on the even or odd number of the iterations.

1 Like

Oh I see. Say I have a code in c

for (int I = 0 ; I < 0 ; I++) {
      #code .. 
      }
      return;

how would i implement the modulo to do something every nth time, say every odd?
thanks a lot btw :slight_smile:

or in python:

for i in range(10):
   print(i)

you could use a conditional (if) and then check if even (== 0) or odd (!= 0)

1 Like

Okay thanks, that makes more sense, I thought there was a way in which the a mod would be used inside the for condition statement.