I%0o11 == 0: need an explanation


Hi, with this question. I understand that i starts at 5 and during the loop it will add 1 each time and it will break when this gets met. i%0o11 == 0: However I don’t understand what this means. Can someone explain?

I have added comments to the code to help:

i = 5
while True:              # This starts an infinite loop.
    if i % 0o11 == 0:    # This line checks if i is divisible by 9 (octal 11 is decimal 9) with no remainder.
        break            # If i is divisible by 9, exit the loop.
    print(i)             # Print the current value of i.
    i += 1               # Increment i by 1.

The octal number 0o11 in Python is equivalent to the decimal number 9. Therefore, the condition i % 0o11 == 0 checks if i is divisible by 9 with no remainder.
When i becomes 9, the if statement’s condition will be true, the break statement will be executed, and the loop will terminate.
Hence, the output will be 5, 6, 7, 8 printed on separate lines.

1 Like

How do I know what the decimal equivalent of an octal number is? Should I know all of them off by heart? and which part of the Python course covers this so I can review it.

You can use a programmers calculator or look it up. Though it is good to have some knowledge of hexadecimal and octal numbers.

@Layla If you’re still having difficulty with these numbers, please read this page which should make it clearer.