What do numbers starting with 0 mean in python?

后端 未结 9 2079
忘掉有多难
忘掉有多难 2020-11-22 05:34

When I type small integers with a 0 in front into python, they give weird results. Why is this?

>>> 011
9
>>> 0100
64
>>> 027
23
<         


        
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 05:35

    The latest Python specification for number literals is in PEP-3127 "Integer Literal Support and Syntax"

    The syntax for 011 has been removed in Python 3.0. The syntax for 0o11 has been added in Python 2.6 and it is the only one supported after Python 3.0. There is no __future__ import in Python 2 that would disallow or atleast warn on number literals with leading zeros, so developers have to know what 011 means and that it should be avoided (and that it can actually be avoided).

提交回复
热议问题