What do numbers starting with 0 mean in python?

后端 未结 9 2257
忘掉有多难
忘掉有多难 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条回答
  •  庸人自扰
    2020-11-22 06:00

    In Python 2 (and a few more programming languages), these represent octal numbers.

    In Python 3, 011 no longer works and you would use 0o11 instead.

    In response to edit: and they are regular integers. They are just specified different way; and they are automatically converted by Python to an internal integer representation (which is base-2 actually, so both 9 and 011 are internally converted to 0b1001).

提交回复
热议问题