Tuesday, September 18, 2012

Python 2 vs. Python 3

This is a summary from Langtangen's (2012) latest edition of A Primer on Scientific Programming with Python (found in the preface).  Currently, there are two versions of Python generally called Python 2 and Python 3.  You may have a version such as Python 2.7 or 3.2, etc.  Python 3 is an updated Python language which has some different syntax notation from Python 2.  However, some mathematical software for Python has not been ported to use with Python 3.  Langtangen provides one suggestion which is to use the latest 2, 2.7, and then use the translation tool 2to3 to obtain the code for version 3.

Langtangen also suggests to use the latest syntax and modules which will make the differences between 2 and 3 small.  This is the format Langtangen uses for his book.  He lists two distinct differences in the book's programs:

1. a/b implies float division in version 3 if a and b are integers
2. print 'Hello' in version 2 must be converted to a function for 3 such as print('Hello')

Langtangen states that users using version 3 should have any major issues studying the books version 2.7 program codes.

Langtangen, H. P. (2012). A Primer on Scientific Programming with Python (3rd ed.). Heidelberg: Springer. doi:10.1007/978-3-642-30293-0

No comments:

Post a Comment