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

David Malcolm dmalcolm at redhat.com
Mon Feb 8 16:14:08 EST 2010


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




More information about the Python-list mailing list