2

Why, like in the following python code, does 00100 equal 64?

>>> i = 00100
>>> i
64
>>> type(00100)
<type 'int'>
>>> str(00100)
'64'
>>> str("00100")
'00100'
>>> int(str("00100"))
100
>>> 
user478514
  • 3,859
  • 10
  • 33
  • 42
  • 1
    I do not know Python but looks like `i = 00100` is interpreted as Octal. so, `1*8^2+0+0 = 64`. – Nishant Mar 09 '11 at 09:53

3 Answers3

9

its an octal value because of leading zeros

http://www.translatorscafe.com/cafe/units-converter/numbers/calculator/octal-to-decimal/

^calculator (hard to summarize)

n00b
  • 5,642
  • 2
  • 30
  • 48
  • 7
    As an aside, note that in Python 3 this would be a syntax error, because the syntax for octal literals has changed. See http://stackoverflow.com/questions/1837874/invalid-token-when-using-octal-numbers – jchl Mar 09 '11 at 10:40
2

In Python (and other languages too) when a number begin with 0 is interpreted as an octal number.

Terseus
  • 2,082
  • 15
  • 22
  • @n00b32 Hey, it happens. Sometimes it's because someone was typing the same time as you, or looking up a link, or sometimes they think they can answer the question more clearly or with extra information. You often get more upvotes if you win the race, but that's not everything. – Rup Mar 09 '11 at 10:08
  • i know, i dont downvote if some one answers within 5 minutes... i just remind ;) – n00b Mar 09 '11 at 13:28
1

It's octal. http://en.wikipedia.org/wiki/Octal

1 is 01, 2 is 02, ..., 7 is 07, 8 is 10 (yes!), 9 is 011, etc.

genobis
  • 1,081
  • 9
  • 13