2010 January 13 / j d a v i s @ c a r l e t o n . e d u
Computers and software are complicated and don't always work the way that you expect. As the term progresses, this page will collect tips for dealing with technical issues in the course. If you have an item that you think should be listed here, please e-mail me.
Our textbook refers to version 3.0 of Python. Our lab computers have version 2.5 installed. If you have recently bought a computer, you may find that version 2.6 is installed there. Python 3.0 is somewhat incompatible with programs written for earlier versions of Python (which is why our department and many other organizations have not yet upgraded to it). Here are a few incompatibilities that you may encounter as you read the textbook.
/denotes division of floating-point numbers and
//denotes division of integers (with the remainder thrown away, so that the answer is an integer). In Python 2.x,
/is used for both, and Python figures out which to apply from context. Recommendation: Always use
/, but be aware that division of integers throws away the remainder.
print("Hello, world."). In Python 2.x,
print "Hello, world.". Recommendation: Don't use parentheses.
Python is unusual among programming languages in its sensitivity to white space (a catchall term meaning spaces, tabs, carriage returns, etc. — all characters that print as blank space). In particular, the indentations in a Python program are significant to the meaning of the code, and improper indentation can lead to unexpected syntax errors. The most common problem occurs when some of your tabs are expanded (as sequences of spaces) and others of your tabs are unexpanded. For example, here I am editing some Python code in TextWrangler. You don't have to understand the code here; just look at the indentation.
Everything looks good, but the code isn't working as I expect. Based on prior experience, I suspect that the indentations may be messed up. So in TextWrangler I go to the View menu, then Text Display, then select Show Invisibles. Here's what happens.
Now certain invisible white space characters, such as carriage returns and tabs, are made visible, after a fashion. We see that each line ends in a carriage return. We also see that the indentations on lines 22 and 23 are different from the others; they are actually made of tabs. I fix this by manually changing each tab to four spaces. I save and re-run the program, and it works.