Invalid SyntaxError Token

I have a problem when I try to assign a value to a variable. The problem appears when I try to put the date as a tuple or list in the following 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 
  1. Why is this happening?

  2. How can I fix this?

  3. What does a token mean in Python?

+27
python syntax
Apr 03 '16 at 14:01
source share
5 answers

In Python 3, leading zeros are not allowed to numbers. For example:

 05 0123 

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

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

In Python 3, the octal syntax has changed to this:

 0o10 0o4 

(and also allow other bases, such as binary and hexadecimal, using the 0b or 0x prefixes.)

As for your other question, the token in Python is how the Python interpreter breaks your code into pieces so that it can understand it (see here ). Here, when the tokenizer tries to split your code, it does not expect to see zero there and therefore throws an error.

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

+46
Apr 03 '16 at 14:16
source share

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

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

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

+14
Apr 3 '16 at 2:08
source share

The problem is 0 before 4. If you want to keep this information, 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 it is interpreted as an octal number. In Python 3, octal numbers are written in the form 0o4, as written here: http://docs.python.org/3.0/whatsnew/3.0.html#integers

+2
Apr 03 '16 at 2:06
source share

In Python version 2.7, we get an error when we use 0 before any number, and this number is not valid in the octal number system. For example, if we use 08 or 09, we will encounter the same error "invalid token."

The Python interpreter divides the entire script into various parts, and these parts are called tokens. Here, 08 will be considered a token, and therefore, it is octal and invalid in this number system, so this kind of error occurs.

Can you try a simple statement like a = 04 and indicate the result? If this works and fails only when using a tuple or list, then this may be a problem with a specific version of Python. If this does not work, then something is wrong with the configuration of your machine. In this case, you can upgrade the version of Python if you are using an older version.

+2
Apr 17 '18 at 21:19
source share

When we install the module, sometimes the error shows an invalid SyntaxError token, and then use the following command

pip install --upprade pip

after that install the module using two commands

easy_install package_name

pip install package_name

-2
Jun 13 '17 at 10:11
source share



All Articles