I know this is going to sound silly but I cannot for the life of me figure out the logic behind how this for loop returns 13,11,9,7.
for i in range(13,5,-1):
if i % 2 != 0:
print i
I know the first value is the number it starts with, the second is where it stops, and the third being the steps it takes. The “if i % 2 !=0:” is what is throwing me off. Can anybody explain what is happening for me?
the first bit is the
range(13,5,-1)which just counts backwards from13to6. The next bit isi%2 != 0.i%2 == 0is equivalent to sayingif even, or “if this number can be divided by 2 with no remainder”, so your statment is saying “if odd” (which is obviously the same as “if not even“).Basically, the loop is printing odd numbers starting at 13 and decreasing down to 6 (but 6 is even, so it doesn’t get printed)