0

In normal math notations leading zeros do not matter in the non decimal part.

For ex.

00003123 = 3123

031 + 2 = 33

However, in python 3, if I try to do a similar operation, I find interesting results that doesn't make sense. What is happening in Python if the numbers are represented with a/many leading zeros?

Sample example in python

>>> 00003123
1619
>>> 031+2
27

3 Answers3

2

Literals beginning with zero in python 2 are octal. For example, octal 31 is 25 in base 10.

For a more complete answer, see What do numbers starting with 0 mean in python?

bartonstanley
  • 1,167
  • 12
  • 25
1
3 x 512 + 1 x 64 + 2 x 8 + 3 = 1619
3 x 8 + 1 + 2 = 27

This is the octal representation, now pretty outdated. Hexadecimal is preferred.

0

The leading 0 caused Python to interpret the number as an Octal (base 8) so 31 in base 8 is actually 25 in base 10

KJTHoward
  • 806
  • 4
  • 16