0
from datetime import datetime
datetime(2018,01,01,10,08,00)

The above code produces an error

>>> datetime(2018,01,01,10,08,0)
SyntaxError: invalid token

If i change 08 to any value between 01 and 07 then the error will not show and also the error will unshown if i change 08 to 8.

What is the reason behind it?

mcv
  • 45
  • 2
  • 10
  • Time to upgrade to Python 3 - this form of octal literals is gone in the current version of the language. (instead of `010` you'd have to write `0o10`). – Kos May 11 '18 at 10:42

1 Answers1

2

Because integer literals starting with a 0 are interpreted as octal numbers and the digit 8 is not allowed in an octal number.

Strip your leading zeros. They have more meaning than you expected.

Alfe
  • 56,346
  • 20
  • 107
  • 159