python - Python3: What this "%" means in this code? -


this question has answer here:

i'm study python 3, , % in code, see below:

def main():     maxwidth = 100 # limita o numero de caracteres numa célula     print_start() # chama função print_start     count = 0 # cria uma variavel cont  while true:     try:         line = input()         if count == 0:             color = "lightgreen"         elif count % 2:             color = "white"         else:             color = "lightyellow"         print_line(line, color, maxwidth)         count += 1     except eoferror:         break print_end() # chama função print_end 

what elif count % 2: line means?

this called modulo or modulus operator.

it divides "left" value "right" value , returns remainder (the amount left on after division).

it's commonly used solve problem of doing every n iterations or loops. if wanted print message every 100 loops, this:

for in xrange(10000):     if % 100 == 0:         print "{} iterations passed!".format(i) 

i see:

0 iterations passed! 100 iterations passed! 200 iterations passed! 300 iterations passed! 400 iterations passed! 500 iterations passed! ... 

in code, if count % 2 act on every other iteration: 1, 3, 5, 7, 9. if count 0, 2, 4, 6, or 8, count % 2 return 0 , expression false.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -