Python 2 vs Python 3 for teaching

Chris Angelico rosuav at gmail.com
Sun Nov 1 04:43:14 EST 2015


I'm proud to say that a Python tutoring company has just converted its
course over from teaching Python 2.7 to teaching 3.x. For the
naysayers out there, it actually wasn't much of a transition; putting
parentheses around all print calls, plus changing the way virtual
environments get created, pretty much covered it. The difference
between well-written 2.7 code and well-written 3.4 code is really not
huge.

Interestingly, the bytes/unicode distinction wasn't much of an issue.
Under 2.7, a lot of functions return Unicode strings, and their reprs
show a u prefix, which disappears under Py3. In both cases, a Unicode
string returned from a library will compare equal to a simple
double-quoted string:

>>> import json
>>> json.loads('["Hello", "World"]')
[u'Hello', u'World']
>>> _[0] == "Hello"
True

The bulk of the changes were actually just changing displayed output
to match a change to some object's repr (eg "<type 'bool'>" becomes
"<class 'bool'>", and "set([1])" becomes "{1}"), or the exact text of
an exception (the TypeError from evaluating None[0] looks different,
but it's still a TypeError).

Who out there is currently teaching/tutoring/training using Python 2?
Push to the common subset (parenthesized single string prints, never
assuming int/int yields int, etc), with a view to migration - it's
easier than you might think!

(This isn't meant to be an ad for a specific company, so much as a
general recommendation to push to Py3, but they deserve a bit of a
shout-out anyway. The company is Thinkful, www.thinkful.com.)

ChrisA



More information about the Python-list mailing list