tags:

views:

96

answers:

6

zipcode = 02132

print zipcode

result = 1114

+13  A: 

A leading zero means octal. 2132 in octal equals 1114 in decimal. They removed this behavior in Python 3.0.

Juri Robl
+4  A: 

The leading 0 makes it assume 02132 is octal.

egrunin
+2  A: 

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

Peter Smit
+5  A: 

In Python 2.x, number with a leading zero is interpreted as octal (base-eight). Python 3.x requires a leading "0o" to indicate an octal number. You probably want to treat a zipcode as a string to keep the leading zeroes intact.

casevh
+3  A: 

leading zero means octal as other have said. one way to keep your zero is to strip the leading zeros and just use a zero padded string when you display it,


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

you probably get the idea.

blackkettle
+4  A: 

Quite apart from the octal caper:

Zip codes, social security "numbers", credit card "numbers", phone "numbers", etc are NOT numbers in the sense that you can do meaningful arithmetic on them, so don't keep them as integers, keep them as strings.

John Machin
Does checksumming count as "meaningful arithmetic" for credit card numbers? Moreover, why use 16 bytes when you can use 8, especially if you have guarantees about no leading zeroes? Arithmetic on RDBMS primary keys is seldom meaningful, should we make them strings too?
p00ya
@pooya: checksumming is not arithmetic like the plus/minus of integers; it's symbolic arithmetic on a string of symbols. 16 bytes for a credit card number? I dunno why either, I'd use packed decimal.
John Machin