Rob Volgman - 1 year ago 47

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 Source

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.