5

Why a number like 01 gives a Syntax error when 01 is typed in python interactive mode and pressed enter?

When 00 is entered the interpreter evaluates to 0, however numbers like 01, 001 or anything which starts with a 0 is entered Syntax error:invalid token is displayed.

Entering 1,000 in prompt evaluates to a tuple of (1,0) but 1,001 doesn't evaluate to (1,1) instead Syntax error is displayed.

Why does the Python interpreter behave so?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
Pradeesh
  • 51
  • 3
  • It doesnt produce a syntax error when doing 01. The interpreter gives 1. – Henrik Andersson Apr 12 '13 at 08:20
  • @limelights It depends on the version of Python. `2.7` gives 1, `3.2` gives a syntax error for me. – Yuushi Apr 12 '13 at 08:21
  • @Yuushi Ah, I'm not well versed in Python 3 yet. I had no idea that they scrapped octal literals. – Henrik Andersson Apr 12 '13 at 08:22
  • @limelights Neither did I to be honest. – Yuushi Apr 12 '13 at 08:24
  • It is only the leading zero syntax for octal literals that has gone - not octal literals in general. The alternate prefix `0o` (along with `0b` for binary, and, since longer ago, `0x` for hex) is available from late in the 2.x series (I think 2.6, but maybe not till 2.7). – lvc Apr 12 '13 at 08:30

2 Answers2

11

Historically, integer literals starting with zero denoted octal numbers. This has been abolished in Python 3, and replaced with a different syntax (0o...).

The old syntax is no longer accepted, except when the number consists entirely of zeros:

Python 3.3.0 (default, Dec  1 2012, 19:05:43) 
>>> 0
0
>>> 00
0
>>> 01
  File "<stdin>", line 1
    01
     ^
SyntaxError: invalid token
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 3
    +1, and a link to the doc for an explanation: http://www.python.org/dev/peps/pep-3127/#removal-of-old-octal-syntax – Yuushi Apr 12 '13 at 08:23
  • Do you know why it exists when the number consists entirely of zeros? – jamylak Apr 12 '13 at 08:28
  • 1
    @jamylak: I am guessing due to lack of ambiguity (there's no uncertainty about the number denoted by `000000`). Although it's hard to see how that would make any practical difference. – NPE Apr 12 '13 at 08:29
  • @NPE Maybe it's so people can have fun by holding down the `0` key on IDLE. Actually I realized why now, it has lots of real world applications such as digital time – jamylak Apr 12 '13 at 08:33
  • The non-ambiguity argument holds for 01 in the same way as for 00 for all number bases and for upto 07 for previously octal numbers, but this is more a problem of python itself. – guidot Apr 12 '13 at 10:09
2

In Python 2.x, a leading zero in an integer literal means it is interpreted as octal. This was dropped for Python 3, which requires the 0o prefix. A leading zero in a literal was left as a syntax error so that old code relying on the old behavior breaks loudly instead of silently giving the "wrong" answer.

lvc
  • 34,233
  • 10
  • 73
  • 98
  • Do you have idea why the feature might removed?...in most of language 0 prefixed number is Octal.. – Grijesh Chauhan Apr 12 '13 at 08:35
  • @GrijeshChauhan: The rationale is explained in the PEP: http://www.python.org/dev/peps/pep-3127/#removal-of-old-octal-syntax – NPE Apr 12 '13 at 08:41