What do numbers starting with 0 mean in python?

后端 未结 9 2214
忘掉有多难
忘掉有多难 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:50

    I have tried it out. I have learned a little bit. From Java I know this as a pitfall: A leading zero introduces an octal number. I have colleagues which didn't resp. don't know that after more than years with experience in Java.

    Now I was interesting what the behaviour in Python is. I appreciate that change from Python 2 to Python 3. It is pitfall I have never understood why Java (a young language with focus on "beeing easy") could take over that stupid solution from C.

    A leading zero as prefix can be accidentally typed (bad solution). 0x for a hexadecimal number is never accidentally typed (good solution). But 0 and o are to similar therefore I think 0o23 in Python is not a perfect solution. 0c23 (octal) would be a better solution in my opinion.

提交回复
热议问题