Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 08 or 09 in Python invalid? [duplicate]

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 don't seem to work. Are these special values or something?

like image 482
siebz0r Avatar asked Oct 17 '14 11:10

siebz0r


2 Answers

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
like image 137
avi Avatar answered Oct 06 '22 05:10

avi


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

>>> 010
8
like image 29
fredtantini Avatar answered Oct 06 '22 03:10

fredtantini