Can anybody explain me the following code. I did not understand what % does. As I know it returns remainder, but I did not get the output i was expecting. What is remainder itself? can you please explain output step by step.
for num in range(20):
if num % 4 == 0:
print num
if num % 16 == 0:
print('XYZ')
That operator is called the modulus operator, and what it does is, basically, continues subtracting the right hand side from the left hand side until it can’t go any more (So, subtracting again would make the result negative). The number that’s left at the point it cannot subtract anymore is called the remainder.
It’s like doing division, but throwing away the quotient.
Try running the code:
See the pattern?