Quiz on while loop

Hi,
Please explain how is if condition evaluating to False in following code? Since 1 is integer and 0o7 is octal number, how does the expression 1%0o7 evaluate?

i = 1
while True:
if i % 0o7 == 0:
break
print(i)
i += 1

Hi @Sandesh-D-Manocharya

I assume your program is

i = 1
while True:
    if i % 0o7 == 0:
        break
    print(i)
    i += 1

Remember that indentation is part of syntax so when posting code, it is important to use code block as above.

As to your question

  • 0o7 is an octal constant. It is still an integer though with a value of 7 (decimal).
  • % is the modulo (or remainder operator)

So i % 0o7 == 0 simply means that i is exactly divisible by 7

4 Likes