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??
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/
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