Switching from Python V. 2 to V. 3

From dftwiki3
Revision as of 15:48, 8 September 2011 by Thiebaut (talk | contribs)
Jump to: navigation, search

Main Reference

A good description of what is different in Python 3 can be found on python.org, at http://docs.python.org/py3k/whatsnew/3.0.html, from which the code snipets below are taken:

Examples Illustrating the Differences

The most obvious difference is that print is now a function and requires parentheses.

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!


Porting old V2 programs to V3