Understanding Python Numbering

We cannot declare an integer starting with 0.

>>> n = 08 SyntaxError: invalid token 

But we declare a variable containing all zeros.

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

So, the question is in the first case, why python just does not take into account the value of the variable up to n = 8 , ignoring zero on the left side, and not raising an exception. As in the second case, it still considers all zeros in a valid value.

Consider another case.

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

Now in the third case, he still considers this a valid numerical value, why is there no exception here?

+4
source share
4 answers

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

+9
source

In most languages, the prefix 0 indicates octal (base 8).

In python 2.7, I get:

 >>> n = 010 >>> n 8 

I suppose your problem is elsewhere. (If this is a problem with the version, I suppose they decided that almost no one ever wants to hard code the variable in base 8, so they made it a mistake).

- Change:

Indeed, the octal literal format has changed (in version 3.0):

http://docs.python.org/release/3.0.1/whatsnew/3.0.html#new-syntax

Now this:

 >>> n = 0o10 >>> n 8 

(works in version 2.6 +)

+4
source

The question of why the error occurred was answered by others; There was a convention for numeric literals in Python, so literals starting with 0 were considered octal. If you put a digit greater than 7 in such a literal, the result was an error. Then, with Python 3, this convention was changed, and all literals with 0 at the beginning caused an error.

Second question: why does int not throw this error. int does not throw this error because it has its own convention for specifying the base of the string: an optional second argument with a default value of 10. This allows int to accept a wider range of values ​​without any problems for ambiguity. Consider this, for example:

 >>> int('55', 16) 85 >>> int('0x55', 16) 85 

The base is indicated here; there is no ambiguity. In this case, it would be an obstacle to reject strings with the usual 0x at the beginning. Likewise, it would be unreasonable to reject lines with 0 at the beginning, when the base is uniquely 10.

In general, for me it makes sense (for me) that the language be strict with literals, but more flexible with type conversions.

+3
source

I have no problem with this:

 #!/usr/bin/python def test2(): n = 01 print n test2() 

But for this, it outputs 64 (base 8):

 def test2(): n = 0100 print n test2() 

Python 2.6.6

0
source

Source: https://habr.com/ru/post/1394110/


All Articles