What do numbers starting with 0 mean in python?

后端 未结 9 2255
忘掉有多难
忘掉有多难 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 05:53

    They are apparently octal (base 8) numbers, and the 0 is just an outdated prefix that Python 2 used to use.

    In Python 3 you must write: 0o11 instead.

    They are still integers but doing operations with them will give a result in regular base-10 form.

提交回复
热议问题