Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Python Numerology

Tags:

python

We can not declare an integer which start with 0.

>>> n = 08
SyntaxError: invalid token

But we do declare a variable that contains all zeros.

>>> n = 00000
>>> print n
>>> 0

So the question is in first case why python just not consider value of variable to n = 8 by ignoring the zero on left side instead of raising an exception. As in second case it is still considering all zeros to a valid value.

Consider another case.

>>> n = '0008'
>>> print int(n)
>>> 8

Now in third case it is still considering it a valid numeric value, why an exception is not raised here??

like image 421
Aamir Rind Avatar asked Dec 03 '22 05:12

Aamir Rind


1 Answers

Numbers beginning with 0 and containing no decimal point are interpreted as octal (using digits 0-7). 08 is not a valid octal number. According to the PEP Index, "the ability to specify an octal number by using a leading zero will be removed from the language in Python 3.0 (and the Python 3.0 preview mode of 2.6), and that a SyntaxError will be raised whenever a leading "0" is immediately followed by another digit" can be found here http://www.python.org/dev/peps/pep-3127/

like image 82
Frantz Romain Avatar answered Jan 03 '23 04:01

Frantz Romain