Why Python don't accept 03 as a number?
MRAB
python at mrabarnett.plus.com
Fri Dec 7 21:04:27 EST 2018
On 2018-12-08 01:17, jfong at ms4.hinet.net wrote:
>>>> 00
> 0
>>>> 03
> File "<stdin>", line 1
> 03
> ^
> SyntaxError: invalid token
>>>>
>
> Any particular reason?
>
Before Python 3, a leading 0 in an integer literal would indicate an
octal (base 8) number.
In Python 2.7:
>>> 010
8
That notation was borrowed from C.
The hexadecimal (base 16) notation of a leading 0x was also borrowed
from C (it was a later introduction to the language).
Python also has the binary (base 2) notation of a leading 0b.
For Python 3, it was felt that it would be clearer to have an octal
notation that didn't use just a leading 0, so it was changed to a
leading 0o.
The old form is now invalid in order to reduce the chance of bugs.
If you're coming from Python 2, you might expect that 010 is still
octal, and if you're not familiar with that notation, you might expect
that it's decimal.
More information about the Python-list
mailing list