Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a problem when I try to assign a value to a variable. The problem shows up when I try to put a date as a tuple or a list in this order: year, month, day.

>>> a = (2016,04,03)         # I try to put the date into variable 'a' as a tuple.
SyntaxError: invalid token
>>> a = [2016,04,03]         # I try to put the date into variable 'a' as a list.
SyntaxError: invalid token
  • Why is this happing?

  • How do I fix it?

  • What does token mean in Python?

  • Etc. are not allowed, but should be written as 5 and 123 instead.

    In Python 2, however, the leading zero signifies that the number is an octal number (base eight), so 04 or 03 would mean 4 and 3 in octal, respectively, but 08 would be invalid as it is not a valid octal number.

    In Python 3, the syntax for octals changed to this:

    (As well as allowing other bases such as binary and hexadecimal using the 0b or 0x prefixes.)

    As for your other question, a token in Python is the way the Python interpreter splits up your code into chunks, so that it can understand it (see here). Here, when the tokenizer tries to split up your code it doesn't expect to see the zero there and so throws an error.

    I would suggest (similarly to the other answers) that you drop the leading zero ((2016,4,3)) or represent these using strings (("2016","04","03")).

    @user2357112 I mean to say, why not "SyntaxError: invalid syntax" instead "SyntaxError: invalid token" – brainLoop Sep 4, 2018 at 7:51

    04 is a valid integer literal in Python 2.x. It is interpreted as a base-8 (octal) number. 09 would be an invalid token as well, since 9 is not a valid octal digit.

    In Python 3, the form of octal literals changed. A leading zero alone is no longer valid; you need to explicitly specify the base. For example, 0o12 is equal to 10.

    In your case, you probably want to just drop the leading 0: a = (2016, 4, 3). Leading zeros could be added to the string representation of your tuple when necessary, rather than trying to store them explicitly.

    The problem is the 0 before the 4. If you want to store that kind of infos, try using strings.

    a = (2016,04,03) --> Error
    a = (2016,4,3) --> No Error
    a = ("2016","04","03") --> No Error
    a = "2016-04-03" --> you could retrieve Year, Month and Day by splitting this string
    

    In Python 2.x 04 is interpreted as an octal number. In Python 3 octal numbers are written in form 0o4 as written here : http://docs.python.org/3.0/whatsnew/3.0.html#integers

    You should explain why the error is coming up. That is one of the questions by the OP. This does not really help them much. – idjaw Apr 3, 2016 at 14:08

    In python version 2.7, we gets error when we use 0 before any number and that number is invalid in octal number system. For e.g. if we use 08 or 09 then we will encounter the same error 'invalid token'.

    Python interpreter divides the entire script into various parts and those parts are called tokens. Here, 08 will be consider as token and hence it is in octal and invalid in this number system so this kind of error occurs.

    Can you please try to run a simple statement like a=04 and mention the result? If it works and fail only while using tuple or list then it may be the issue with particular python version. If it does not work then there is something wrong with your machine configuration. In this case, you can upgrade your python version if you are using the older version.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.