1

Does anyone why python handles the below this way.

>>> a = 099
  File "<stdin>", line 1
    a = 099
          ^
SyntaxError: invalid token
>>> a = 088
  File "<stdin>", line 1
    a = 088
          ^
SyntaxError: invalid token
>>> a = 0559
  File "<stdin>", line 1
    a = 0559
           ^
SyntaxError: invalid token
>>> a = 077
>>>

It does not seem to accept numbers starting with 0 and preceding with 8 or 9. If it is some other number, it is not throwing any error. Why is that?

SuperNova
  • 25,512
  • 7
  • 93
  • 64

1 Answers1

2

In Python 2, like in C, an integer literal that starts with a 0 is in octal. Digits 8 and 9 do not exist in octal (they are written 010 and 011 respectively) so that is a syntax error.

>>> 010
8
>>> 08
  File "<stdin>", line 1
    008
      ^
SyntaxError: invalid token

In Python 3, this feature not many people know about is gone. There, nonzero literals that start with a 0 are syntax errors.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79