tags:

views:

40

answers:

1

If you simply type an integer after the >>> prompt they give you in the IDLE interpreter, most of the time it'll simply bounce the number back at you.

>>> 3
3
>>> 8
8
>>> 10
10

Start the nubmer off with a 0 however, and some interesting errors happen.

>>> 010
8
>>> 020
16
+12  A: 

In Python 2, an integer literal starting with 0 is considered octal, i.e. in base 8. And obviously, 10 oct == 8 dec (or generally, 10 in base b == b base 10). Likewise, 12 oct == 10 dec, and so on.

delnan
... and changed in Python 3: you must use the "0o..." form for octal literals, introduced in Python 2.6, instead (http://docs.python.org/release/2.6.5/whatsnew/2.6.html#pep-3127-integer-literal-support-and-syntax)
Ned Deily
Hence the old joke: Q. Why do programmers celebrate Christmas at Halloween? A. Because DEC 25 = OCT 31.
Dave Webb