8

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?

siebz0r
  • 18,867
  • 14
  • 64
  • 107
  • 2
    It's octal notation. Also a duplicate of a few hundred questions on SO... Please do some effort searching before asking questions, I literally googled `python 08` and was presented with _multiple_ relevant results from SO. – l4mpi Oct 17 '14 at 11:25
  • They're octal. http://stackoverflow.com/questions/11620151/what-do-numbers-starting-with-0-mean-in-python – Graham Borland Oct 17 '14 at 11:25

3 Answers3

13

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
avi
  • 9,292
  • 11
  • 47
  • 84
2

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

>>> 010
8
fredtantini
  • 15,966
  • 8
  • 49
  • 55
0

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.

Prisoner
  • 49,922
  • 7
  • 53
  • 105