1

I observed this peculiar behavior while trying to assign a integer value to a variable. What is the possible explanation?

>>> a = 01
>>> a = 02
>>> a = 03
>>> a = 04
>>> a = 05
>>> a = 06
>>> a = 07
>>> a = 08
  File "<stdin>", line 1
    a = 08
         ^
SyntaxError: invalid token
>>> a = 09
  File "<stdin>", line 1
    a = 09
         ^
SyntaxError: invalid token

I'm using python 2.7.6, gcc 4.8.2.

vyi
  • 1,078
  • 1
  • 19
  • 46

2 Answers2

4

08 octal is 10 decimal. Write 010.

01 == 1
02 == 2
# ...
07 == 7
010 == 8
011 == 9
012 == 10
# ...
017 == 15
020 == 16
2

A number starting with 0 is an octal literal.

8 is an invalid octal digit, so 08 makes no sense. Ditto 09.

Drop the leading zeros, even if they did make your source code look pretty.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483