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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@Harman you are quite welcome
but If i want to perform any mathematical operations then how can i use 9, 8 number ?
@Harman what sort of math do you need to perform on zip codes?
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 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.