python 2.7 - Explain this bit of code to a beginner -
this question has answer here:
- how % work in python? 17 answers
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
Post a Comment