3

I was playing around with Python. I had a doubt about the power operation in Python. So, I tried this:

0726**13 = 54609997061205831773270000000000000L 
726**13 = 15565965698792536237226936270158258176L

Why is there a difference between these two? I know it might be trivial. But, I could not figure it out. Could someone please explain? Thanks.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253

2 Answers2

10

It's because an integer constant beginning with 0 is taken to be an octal value. In this case, 0726 is interpreted as 470:

>>> 0726
470
>>> 470**13
54609997061205831773270000000000000L
>>> 
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • 1
    Just to make this easier for searching, the problem is occurring because leading zeroes are shorthand in numerous languages for representing octal constants. http://stackoverflow.com/questions/11483216/why-are-leading-zeroes-used-to-represent-octal-numbers – Alex Taylor Dec 10 '15 at 02:49
3

Numbers starting with 0 in Python are represented in Base 8 (octal numbers). That's why you're getting different results.