[Tutor] datetime syntax error for May 8th and 9th 2008??

wesley chun wescpy at gmail.com
Sat May 17 08:38:42 CEST 2008


On Fri, May 16, 2008 at 9:29 PM, John Fouhy <john at fouhy.net> wrote:
> On 17/05/2008, Che M <pine508 at hotmail.com> wrote:
>> >>> datetime.datetime(2008, 05, 08)
>> SyntaxError: invalid token
>
> It's simpler than that... Try this:
>
>>>> x = 08
>  File "<stdin>", line 1
>    x = 08
>         ^
> SyntaxError: invalid token
>>>> x = 010
>>>> x
> 8
>
> Basically, python interprets integer literals starting with 0 as octal
> numbers.  It's an old convention from C (or earlier?).  It doesn't
> affect strings, so int('010') == 10 (unless you use eval).


che,

john is correct.  any leading zero (0) of an integer number will be
translated as base 8 or octal, which counts by 8's:

00, 01, 02, 03, 04, 05, 06, 07, 10 (decimal/base 10: 8*1+0=8),
11 (decimal: 8*1+1=9), 12 (decimal: 8*1+2=10), 13 (decimal 8+3=11),
14, 15, 16, 17 (decimal: 15), 20 (decimal: 8*2+0=16), 21 (decimal:
8*2+1=17), etc...

the problem with "08" is that because you're counting by 8s, a value
of "08" is invalid because the digits "8" and "9" are not part of the
octal "alphabet" or character set. that is why "07" works but not
"08".

as an FYI, because of this confusion, starting with Python 3.0, octal
numbers will require a lowercase "o" (in the same manner as
hexadecimal/base 16 numbers require an "x") after leading 0, i.e.,
0o7, 0o16, etc.  (Oo8 is still an invalid octal number.)

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
 http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list