python 2.7 - Explain this bit of code to a beginner -


this question has answer here:

for x in xrange(12):     if x % 2 == 1:         continue     print x 

i know does, language doesn't make sense me. in particular second line lost.

if x % 2 == 1 means "if x modulo 2 equals 1".

modulo (or mod) remainder after division. so, example:

3 mod 2 = 1 12 mod 5 = 2 15 mod 6 = 3 

for x mod 2, you're there's remainder if , iff x odd. (because numbers divisible 2 0 remainder.) likewise, odd numbers have remainder of 1.

so x % 2 == 1 returns true if x odd.


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 -