3

With Python 3.8, is there a reason why 00 or 000000 are valid while 03 yields an error?

>>> 000
0

03
  File "<stdin>", line 1
    03
     ^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
Philippe Remy
  • 2,967
  • 4
  • 25
  • 39

1 Answers1

1

Yes. Python's constants derived from C, where integers starting with a 0 are interpreted in octal. So, 0377 has the decimal value 255. Over time, Python was changed to the 0o prefix, but C still allows the 0 prefix. To avoid any potential confusion, the 0 prefix is now a syntax error.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • 3
    I think the key issue, though, is why is `00` or `00000` allowed, i.e., leading zeros are not allowed *unless it's all zeros*. See the linked duplicate, it links to various bug reports, it seems, this really was something that slipped passed in the implementation that goes against the PEP that describes the behavior, but it won't be fixed. EDIT: For the record, I didn't downvote, – juanpa.arrivillaga Oct 06 '21 at 06:34
  • reading the duplicate question, the reason seems to be that 0 is less ambiguous than the other numbers as it has the same value in all the bases. some remarked that all the one digit numbers have the same property, but if we consider that no integer should start with 0 then 0 is already an exception. – Jean Paul Apr 19 '23 at 15:04