Since the octal prefix is now 0o
in Python 3 it's not legal to write 0777
any more. Okay.
So why is it legal to write 00
which evaluates properly to 0
whereas other digits trigger a syntax error?
>>> 01
...
File "<interactive input>", line 1
01
^
SyntaxError: invalid token
>>>
>>> 00
0
None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.
The Python Boolean type is one of Python's built-in data types. It's used to represent the truth value of an expression. For example, the expression 1 <= 2 is True , while the expression 0 == 1 is False . Understanding how Python Boolean values behave is important to programming well in Python.
If one takes a look at the Lexical Analysis (Integer Literal Section) page:
integer ::= decinteger | bininteger | octinteger | hexinteger decinteger ::= nonzerodigit (["_"] digit)* | "0"+(["_"] "0")* ...
So that means that a decinteger
either begins with a nonzero digit (followed by all possible digits and optionally underscores), or is a sequence of zeros with optionally underscores (which maps to zero).
The documentation furthermore states that:
Note that leading zeros in a non-zero decimal number are not allowed.
So it means they make an exception for zero (in all documentation for python-3.3 one can find there): you can write zero as a sequence of zeros. My guess is that of course they have to include "0"
(how else would you specify zero as a decinteger
?), so why not allow more zeros in that case, regardless of the number system, 000
is and stays zero. They probably do not want to allow 01
as a decinteger
to prevent accidentally running python-2.x code and thus obtaining totally different results.
Finally note that the underscores are only part of that specification since python-3.6: in the specifications for 3.5 they are not mentioned in the grammar.
In python-2.7 the documentation specifies a zero followed by other digits (also other zeros as an octinteger
:
integer ::= decimalinteger | octinteger | hexinteger | bininteger decimalinteger ::= nonzerodigit digit* | "0" octinteger ::= "0" ("o" | "O") octdigit+ | "0" octdigit+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With