Tuesday, September 18, 2012

Avoid integer division when programming

I didn't realize I was doing this but noticed I did have a problem for one of my Python scripts.  I was doing 1/2 which is apparently a no-no in programming.  I fixed it by doing 0.5*...  Other ways include 1.0/2 etc.

Commenting in Python

To comment in Python simply use the pound sign, #, in front of what ever it is you would like to comment.  For Spyder users, the shortcut to this in the editor is Crtl+1.

Reserved words in Python

If you've been experimenting with Python, as I have, then you might notice that some words are saved for some certain processes.  For example lambda is one of these words.  I tried to use it as a variable name.  it will usually highlight in blue if it is important.  So an alternative is to use lamb_da or lamb_duh etc.

Other words include:

and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, with, while,and yield.

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

Differences between Python (scripting programming) and traditional programming C/C++/Fortran etc. (system programming)

This post is a summary from Langtangen's (2009) book Python Scripting for Computational Science (Chapter 1 Introduction).  Langtangen breaks down the programming languages into scritpting and traditional types.  Langtangen comments that these are quite different in programming style and are usually geared towards different goals.  The traditional programming languages are also known as system programming and are used for building (usually large, monolithic) applications (systems).  These languages include Fortran (77/90/95/2003), C, C++, C#, and Java.  Langtangen refers to scripting as programming at a high and abstraction level.  These languages include Perl, pythin, Ruby, Scheme, and Tcl.

Langtangen, H. P. (2009). Python Scripting for Computational Science (3rd ed.). Berlin: Springer. doi:10.1007/978-3-540-73916-6

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