1

Today I am learning python and I have assign one int variable in console like this

zipcode = 02492

But it's return me error like this

SyntaxError: invalid token

Why so, I don't understand it? Please help me to solve this query.

JohnE
  • 29,156
  • 8
  • 79
  • 109
Harman
  • 1,703
  • 5
  • 22
  • 40

1 Answers1

3

The reason you're getting the error is because Python interprets a number starting with the digit 0 as octal (base 8). However, the only valid octal digits are 0-7, so the 9 in your zip code is seen as invalid. Additionally, if you're using Python 3, the format of octal literals was changed so they begin with 0o now (a zero followed by the lowercase letter o), so you'd still get an error even if you tried to input zipcode = 02432, which would be valid in Python 2.

Since a zip code doesn't need to mathematical operations performed on it, it would be best if it was stored as a string.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 1
    @Harman you are quite welcome – MattDMo Jul 05 '14 at 05:34
  • but If i want to perform any mathematical operations then how can i use 9, 8 number ? – Harman Jul 05 '14 at 05:35
  • @Harman what sort of math do you need to perform on zip codes? – MattDMo Jul 05 '14 at 05:36
  • Not In zip code, I am new in python, I just want to know I need to perform any mathematical operation then how can I use 9 or 8 Number in int variable? – Harman Jul 05 '14 at 05:38
  • 1
    @Harman in that case, just assign like usual: `parts = 79345`. It's just when you have a `0` at the beginning that Python thinks it's an octal number. – MattDMo Jul 05 '14 at 05:39