Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 000 evaluate to 0 in Python 3? [duplicate]

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
like image 801
Jean-François Fabre Avatar asked Mar 28 '17 14:03

Jean-François Fabre


People also ask

Is none same as 0 in Python?

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.

Does 1 == true evaluate true or false in Python?

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.


Video Answer


1 Answers

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+
like image 79
Willem Van Onsem Avatar answered Sep 19 '22 14:09

Willem Van Onsem