Skip to content Skip to sidebar Skip to footer

Why Is 08 Or 09 In Python Invalid?

In the Python interpreter, 08 and 09 seem invalid. Example: >>> 01 1 >>> 02 2 >>> 03 3 >>> 04 4 >>> 05 5 >>> 06 6 >>>

Solution 1:

A number with a leading zero is interpreted as octal literal. So 8 and 9 are invalid in octal. Only digits 0 to 7 are valid.

Try in interpreter:

>>>011
9
>>>012
10
>>>013
11

Solution 2:

If a number starts with 0, it means it's an octal number:

>>>010
8

Solution 3:

In Python (along with many other C-origin languages), a leading 0 (and, increasingly a leading 0O) indicate that the number is octal, not decimal. See https://docs.python.org/2/reference/lexical_analysis.html#integer-and-long-integer-literals for details.

For example, and for kicks, see what 010 evaluates to.

Post a Comment for "Why Is 08 Or 09 In Python Invalid?"