Awful book warning: How to think like a (Python) programmer - non-working examples

Discussion in 'Python' started by Dave Peterson, Feb 8, 2010.

  1. Page 7: Very first example doesn't compile: syntax error
    Pate 11: 2nd example: syntax error
    Page 12, printing digits: syntax error
    Page 13, printing a number: syntax error
    page 14, statements: syntax error
    Dave Peterson, Feb 8, 2010
    #1
    1. Advertising

  2. Re: Awful book warning: How to think like a (Python) programmer -non-working examples

    On Mon, Feb 8, 2010 at 3:36 PM, Dave Peterson <> wrote:
    > Page 7: Very first example doesn't compile: syntax error
    > Pate 11: 2nd example: syntax error
    > Page 12, printing digits: syntax error
    > Page 13, printing a number: syntax error
    > page 14, statements: syntax error
    >


    Let me guess, you're using Python 3.1. That book was written for
    Python 2.x and there were several backwards-incompatible changes. For
    instance, print was changed from a statement to a function. Which is
    why the "Hello, World" doesn't work any more. If you want to use the
    older books, use Python 2.6 instead.

    >
    >
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Benjamin Kaplan, Feb 8, 2010
    #2
    1. Advertising

  3. Dave Peterson

    Robert Kern Guest

    Re: Awful book warning: How to think like a (Python) programmer -non-working examples

    On 2010-02-08 14:36 PM, Dave Peterson wrote:
    > Page 7: Very first example doesn't compile: syntax error
    > Pate 11: 2nd example: syntax error
    > Page 12, printing digits: syntax error
    > Page 13, printing a number: syntax error
    > page 14, statements: syntax error


    This book was written for the 2.x versions of Python. Are you using Python 3.1?
    Python changed some of its syntax for version 3.0, notably

    print "Hello, world!"

    becomes

    print("Hello, world!")

    This accounts for all of the SyntaxErrors that you are seeing. The examples
    aren't broken for the version of Python it is teaching.

    You may want to try _Dive Into Python 3_ to learn about Python 3 in particular:

    http://diveintopython3.org/

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Feb 8, 2010
    #3
  4. Re: Awful book warning: How to think like a (Python) programmer -non-working examples

    The book covers Python 2.x syntax.

    You might have downloaded Python 3.1, which has different syntax then
    Python 2.x. From what I can tell, the first example on page 7 is ">>>
    print 1 + 1".

    Try issuing this command:
    print(1 + 1)

    If everything goes well, and you get '2' as the answer, then you're
    probably using Python 3.x. You will have to download the Python 2.x
    binaries from the Python website, install Python 2.x, and try the
    example from the book again.
    Andrej Mitrovic, Feb 8, 2010
    #4
  5. Re: Awful book warning: How to think like a (Python) programmer -non-working examples

    On Mon, 2010-02-08 at 12:53 -0800, Andrej Mitrovic wrote:
    > The book covers Python 2.x syntax.
    >
    > You might have downloaded Python 3.1, which has different syntax then
    > Python 2.x. From what I can tell, the first example on page 7 is ">>>
    > print 1 + 1".
    >
    > Try issuing this command:
    > print(1 + 1)
    >
    > If everything goes well, and you get '2' as the answer, then you're
    > probably using Python 3.x. You will have to download the Python 2.x
    > binaries from the Python website, install Python 2.x, and try the
    > example from the book again.


    Sorry to nitpick; the main thrust of the above sounds correct, in that:
    print 1 + 1
    works in Python 2 but fails in Python 3, but, a minor correction, note
    that:
    print(1+1)
    does work in Python 2 as well as in Python 3; the parentheses are
    treated (in the former) as denoting grouping of a subexpression, rather
    than function invocation (in the latter):

    Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47)
    [GCC 4.4.2 20100121 (Red Hat 4.4.2-28)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print(1+1)

    2

    This can be useful if you're trying to write short fragments of code
    that work with both.

    Look at the startup message, or run this command, which should work on
    both python2 and python3:
    import sys; print(sys.version)

    Hope this is helpful
    Dave
    David Malcolm, Feb 8, 2010
    #5
  6. Re: Awful book warning: How to think like a (Python) programmer -non-working examples

    On Feb 8, 10:14 pm, David Malcolm <> wrote:
    > On Mon, 2010-02-08 at 12:53 -0800, Andrej Mitrovic wrote:
    > > The book covers Python 2.x syntax.

    >
    > > You might have downloaded Python 3.1, which has different syntax then
    > > Python 2.x. From what I can tell, the first example on page 7 is ">>>
    > > print 1 + 1".

    >
    > > Try issuing this command:
    > > print(1 + 1)

    >
    > > If everything goes well, and you get '2' as the answer, then you're
    > > probably using Python 3.x. You will have to download the Python 2.x
    > > binaries from the Python website, install Python 2.x, and try the
    > > example from the book again.

    >
    > Sorry to nitpick; the main thrust of the above sounds correct, in that:
    >     print 1 + 1
    > works in Python 2 but fails in Python 3, but, a minor correction, note
    > that:
    >     print(1+1)
    > does work in Python 2 as well as in Python 3; the parentheses are
    > treated (in the former) as denoting grouping of a subexpression, rather
    > than function invocation (in the latter):
    >
    > Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47)
    > [GCC 4.4.2 20100121 (Red Hat 4.4.2-28)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.>>> print(1+1)
    >
    > 2
    >
    > This can be useful if you're trying to write short fragments of code
    > that work with both.
    >
    > Look at the startup message, or run this command, which should work on
    > both python2 and python3:
    >   import sys; print(sys.version)
    >
    > Hope this is helpful
    > Dave


    Oops, you're right. I'm used to Python 3 syntax so I'm only aware of
    some basic differences. :)
    Andrej Mitrovic, Feb 8, 2010
    #6
    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. DJS2C

    My Site is Awful :(

    DJS2C, Dec 2, 2004, in forum: HTML
    Replies:
    9
    Views:
    453
  2. Dano
    Replies:
    13
    Views:
    610
    Beauregard T. Shagnasty
    Apr 1, 2005
  3. Simon Brunning

    Python book for a non-programmer

    Simon Brunning, Nov 25, 2005, in forum: Python
    Replies:
    8
    Views:
    353
    Steve
    Nov 25, 2005
  4. Replies:
    0
    Views:
    586
  5. Aaron Gray
    Replies:
    9
    Views:
    128
    Yaser Sulaiman
    Nov 11, 2008
Loading...

Share This Page