Python 2.x and 3.x usage survey

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jan 1 06:37:45 EST 2014


Steve Hayes wrote:

> I borrowed a book called "Learning Python" by Lutz and Asher, which is
> geared for 2.2/2.3.
> 
> But the version I have in Windows is 3.2, and it seems that even "Hello
> World" presents and insurmountable problem.

It certainly is not *insurmountable*. Not unless you consider typing
brackets ( ) to be an inhumanly difficult task, in which case you might as
well give up on being a programmer and take up something easier like brain
surgery.

# Python 2 version
print "Hello World!"

# Python 3 version
print("Hello World!")



> Eventually I discovered that one of the differences bytween 2.x and 3.x is
> that the former has "print" and the latter has "print()" but weven using
> that it tells me it cant find the PRN device or something.

Possibly you're trying to run print("Hello World") at the DOS command prompt
rather than using Python. I'm not sure exactly what you're doing, but I do
know that you shouldn't get any errors about the PRN device from Python.
That sounds like it is a Windows error.


> I've got 2.x on Linux, so I booted into that and it seemed to work there,
> but it seems that the differences between the versions are not trivial.

For the most part, they are trivial. With only a few exceptions, everything
in Python 2 can be easily, even mechanically, translated to Python 3.

Python 3 includes a lot of new features that a Python 2.3 book won't even
mention. But if course, since the book doesn't mention them, you won't need
to deal with them. It also includes a few changes from statements to
functions, like print and exec (but as a beginner, you shouldn't be using
exec). A few modules have been renamed. In my personal opinion, the most
annoying change from Python 2 to 3 is renaming modules, because I can never
remember the new name.

None of these are *difficult* changes. As a beginner, of course, you cannot
be expected to automatically know how to deal with a problem like this one:

py> from StringIO import StringIO
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'StringIO'


But let me give you a secret known only to a few: to solve this problem is
not hard. Just google for "StringIO renamed Python 3". which will take you
to the "What's New in Python 3" document, which reveals that the StringIO
module is renamed to io.StringIO, and so you should use this instead:

from io import StringIO


https://duckduckgo.com/?q=StringIO%20renamed%20Python%203


If googling fails, feel free to ask here!


-- 
Steven




More information about the Python-list mailing list