Rob Volgman - 4 months ago 6

Python Question

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

Note: Python version 2.7.3

I have tested this in Python 3.0, and apparently this is now an error. So it is something version-specific.

Edit: they are apparently still integers:

`>>> type(027)`

`<type 'int'>`

Answer

These are numbers represented in base 8 (octal numbers). For example,

`011`

is equal to `8**1 + 8**0`

= 9,

`0100`

is equal to `8**2`

= 64,

`027`

is equal to `2*8**1 + 7*8**0`

= 16 + 7 = 23.

Source (Stackoverflow)

Comments