Why are 08 or 09 not valid in Python?

In the Python interpreter, 08 and 09 seem invalid. Example:

 >>> 01 1 >>> 02 2 >>> 03 3 >>> 04 4 >>> 05 5 >>> 06 6 >>> 07 7 >>> 08 File "<stdin>", line 1 08 ^ SyntaxError: invalid token >>> 09 File "<stdin>", line 1 09 ^ SyntaxError: invalid token 

As you can see, only 08 and 09 do not seem to work. Are these special meanings or something else?

+7
python
source share
3 answers

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

Try the interpreter:

 >>> 011 9 >>> 012 10 >>> 013 11 
+10
source share

If the number starts with 0, it means that it is an octal number:

 >>> 010 8 
+2
source share

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

For example, and for strokes, see what evaluates 010.

0
source share

All Articles