eval('07') works, eval('08') fails, why?

Discussion in 'Python' started by Alex van der Spek, Jan 8, 2009.

  1. I am baffled by this:

    IDLE 1.2.2 ==== No Subprocess ====
    >>> input()

    07
    7
    >>> input()

    08
    Traceback (most recent call last):
    File "<pyshell#1>", line 1, in <module>
    input()
    File "<string>", line 1
    08
    ^
    SyntaxError: invalid token

    of course, I can work around this using raw_input() but I want to
    understand why this happens. It boils down to:

    >>> eval('07')

    7
    >>> eval('08')

    Traceback (most recent call last):
    File "<pyshell#3>", line 1, in <module>
    eval('08')
    File "<string>", line 1
    08
    ^
    SyntaxError: invalid token

    I can't think of anything that could cause this. Similarly, eval('09')
    fails, but for string 0x with x<8 it works. I am teaching myself Python
    in order to climb the ladder from Algol(1980s)-->Pascal(1990s)--
    >VisualBasic(2000)-->Python. I am a physicist, have programmed computers

    all my life but I won't understand the real tech jargon of present day
    computer science. Please keep it simple

    Thanks in advance,
    Alex van der Spek
     
    Alex van der Spek, Jan 8, 2009
    #1
    1. Advertising

  2. Hi,

    07 is octal. That's way 08 is invalid. Try this:

    ===> python
    >>> print 011

    9
    >>> print int('011')

    11




    --
    Thomas Guettler, http://www.thomas-guettler.de/
    E-Mail: guettli (*) thomas-guettler + de
     
    Thomas Guettler, Jan 8, 2009
    #2
    1. Advertising

  3. On Jan 8, 9:31 am, Alex van der Spek <> wrote:

    > >>> eval('07')

    > 7
    > >>> eval('08')

    >
    > Traceback (most recent call last):
    >   File "<pyshell#3>", line 1, in <module>
    >     eval('08')
    >   File "<string>", line 1
    >     08
    >      ^
    > SyntaxError: invalid token


    An integer literal with a leading zero is
    interpreted as an octal (base 8) number,
    so only digits in the range 0-7 (inclusive)
    are permitted in such a literal.

    Mark


    >
    > I can't think of anything that could cause this. Similarly, eval('09')
    > fails, but for string 0x with x<8 it works. I am teaching myself Python
    > in order to climb the ladder from Algol(1980s)-->Pascal(1990s)-->VisualBasic(2000)-->Python. I am a physicist, have programmed computers
    >
    > all my life but I won't understand the real tech jargon of present day
    > computer science. Please keep it simple
    >
    > Thanks in advance,
    > Alex van der Spek
     
    Mark Dickinson, Jan 8, 2009
    #3
  4. Thanks much, that makes sense! Alex van der Spek
     
    Alex van der Spek, Jan 8, 2009
    #4
  5. Alex van der Spek

    Ned Deily Guest

    In article <>,
    Unknown <> wrote:
    > On 2009-01-08, Alex van der Spek <> wrote:
    > > Thanks much, that makes sense!

    > Well, that's the correct explanation.
    > Whether that feature makes sense or not is debatable.


    The debate is over! In Py 3.0, octal literals changed from 07 to 0o7;
    the old format gets an 'invalid token' parsing error.

    --
    Ned Deily,
     
    Ned Deily, Jan 8, 2009
    #5
  6. Alex van der Spek

    Terry Reedy Guest

    Alex van der Spek wrote:

    > I can't think of anything that could cause this. Similarly, eval('09')
    > fails, but for string 0x with x<8 it works. I am teaching myself Python
    > in order to climb the ladder from Algol(1980s)-->Pascal(1990s)--
    >> VisualBasic(2000)-->Python. I am a physicist, have programmed computers

    > all my life but I won't understand the real tech jargon of present day
    > computer science. Please keep it simple


    I taught myself Python as a statistician with a Fortran/C backgound by
    interleaving interactive experiments (such as you did) with reading of
    the manuals. The Language Manual chapter on Lexical Analysis has an
    Integer Literals subsection that answers this question. I strongly
    recommend you peruse the Language Manual and the initial Library Manual
    chapters on built-ins.

    tjr
     
    Terry Reedy, Jan 8, 2009
    #6
  7. Grant Edwards a écrit :
    > On 2009-01-08, Alex van der Spek <> wrote:
    >
    >> Thanks much, that makes sense!

    >
    > Well, that's the correct explanation.
    >
    > Whether that feature makes sense or not is debatable. Even I'm
    > not old-school enough that I ever use octal literals -- and I
    > used Unix on a PDP-11 for years (actually had my own PDP-11 for
    > while, but it never worked). Now that I think of it, my
    > Heathkit Z80 stuff used octal notation too.


    What about your DeathStation 9000 ?-)
     
    Bruno Desthuilliers, Jan 8, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    985
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,237
    Smokey Grindel
    Dec 2, 2006
  3. Jag
    Replies:
    5
    Views:
    342
    Paul Bibbings
    Jul 17, 2010
  4. abargaddon
    Replies:
    1
    Views:
    218
    clintmazur
    Feb 4, 2008
  5. Randell D.
    Replies:
    7
    Views:
    177
    Randell D.
    Oct 26, 2003
Loading...

Share This Page