Not getting exact result in python with values ​​starting from zero. Please tell me what's going on there

zipcode = 02132

zipcode print

result = 1114

+6
python
source share
6 answers

Leading zero means octal. 2132 to octal - 1114 to decimal. They removed this behavior in Python 3.0.

+14
source share

In Python 2.x, a number with a leading zero is interpreted as base-eight. For Python 3.x, you must specify "0o" to indicate an octal number. You probably want to treat zipcode as a string to preserve the original zeros.

+9
source share

In addition to the octal caper:

Postal codes, social security numbers, credit card numbers, phone numbers, etc. They are NOT numbers in the sense that you can do significant arithmetic on them, so do not store them as integers, save them as strings.

+6
source share

The leading 0 suggests that 02132 is octal .

+5
source share

leading zero means octal, as others have said. one way to keep zero is to remove the top zeros and just use the zero filled line when displaying it,

>>> myInt = 2132 >>> print myInt 2132 >>> myString = "%05d" % myInt >>> print myString 02132 >>> print int(myString) 2132 

you probably get this idea.

+3
source share

What is your question? I think why. The answer is octal numbers. If a number starts from zero, Python thinks you mean an octal number. (Base 8)

+2
source share

All Articles