2012 April 19,
CS 111: Technical Issues
Carleton College, Spring 2012
Prof. Joshua R. Davis, , Goodsell 106B, x4473
Summary of Terminal Commands
The files on a computer are organized into folders, which are also called directories. In addition to files that store documents and programs, a directory may also contain other directories, which contain other files and directories. Within a terminal program, you can navigate through this system of files and directories using a few simple commands.
- pwd ("print working directory") tells you which directory you are currently looking at. It also tells you the name of the directory that contains the current directory, and the name of the directory that contains that one, and so on. That is, it tells you all of the directory names, going "up" to the top of the file system.
- ls ("list") lists the files and directories contained in the current directory.
- cd .. ("change directory") changes the current directory to the directory that contains the current directory. In other words, it goes "up" one level in the file system.
- cd directory, where directory is a directory contained in the current directory, changes the current directory to that directory.
- cd (with no directory specified) changes the current directory to your home directory, no matter where you are.
- python2 program.py runs the Python interpreter on the program program.py in the current directory.
- python2 launches the Python interpreter in interactive mode.
- If you are in the interactive Python interpreter, and you want to exit back to bash, then press Control-D.
- If you are in the interactive Python interpreter, and you are running code that isn't going to stop, and you want to stop the code but remain in the interactive Python interpreter, then press Control-C.
Submitting Work Electronically
To submit your work electronically:
- Make sure that all required names (you, your partners, and anyone else who contributed) are on every part of the work — for example, in a comment at the top of every Python program file. Your work group does not hand in multiple copies of the same work; only one of you hands in the work, for all of you.
- Mount the COURSES file server. If you're on a lab machine, you can do this by clicking on the Mount Network Drives icon in the dock. If you are on some other machine, then follow these directions.
- In the Finder (or whatever the file browser is, on the machine you're using), navigate to the COURSES file server, and then to your CS 111 folder, and and handin folder therein. Drag your files to the handin folder, to submit them for grading. If your work is spread over multiple files, or imports modules that are not built into Python, then include those files as well. The handin folder should contain everything needed to run your program.
Warning: Once you have placed a file on the COURSES file server, you cannot edit it or remove it. So make sure that your files are exactly as you want, before you submit them.
Three Versions of Python
Python is constantly growing and evolving. Each new version of Python adds new features. Python 3 not only added new features, but also broke compatibility with earlier versions of Python. Right now, the worldwide Python community is in a transitional phase, where a lot of Python 2-based software has to be rewritten to work with Python 3. Unfortunately, this transition complicates your life a bit. You may run into three different versions of Python:
- Python 2.6 is what you get if you run the command python on our lab machines. The Mac OS X operating system uses this old version of Python for various purposes, so we can't get rid of it.
- Python 2.7 is what you get if you run the command python2 on our lab machines. This is the version of Python that we use in CS 111. It works very well.
- Python 3.3 is what you get if you run the command python3 on our lab machines. This is the version of Python used by our textbook. It's nice, but we don't use it yet, because a lot of our software doesn't work with it.
So what do you need to know and do? It's pretty simple, actually.
- When running Python on our lab machines, always use python2.
- When our textbook uses print, it's always with parentheses, as in print("Hello, world."). This is a Python 3 thing. We always use print without parentheses, as in print "Hello, world.".
- In our textbook, / denotes division of floating-point numbers and // denotes division of integers (with the remainder thrown away, so that the answer is an integer). This is a Python 3 thing. We always use /, for both kinds of division. You and Python have to figure out which kind is relevant, from context.
- If you download Python to your own computer, then you should download Python 2.7 (not 2.6 or 3.3). When you run this Python on your own computer, you will probably type python, rather than python2.
For a full list of changes between Python 2 and Python 3, see What's New in Python 3.0.
Python Programs Opening in IDLE
I recommend that you edit your programs in TextWrangler and run them in the terminal, as described in Intro to Python, Part B. If your Carleton lab computer account (or your personal computer) is set up "correctly", then double-clicking on any Python program file should open that file in TextWrangler.
For some students, Python program files instead open by default in IDLE, which is an integrated editor-and-runner for Python programs. If you have this problem, and you want to change your default to TextWrangler, then perform the following steps. (These instructions are for Mac OS X; a similar procedure surely exists for Windows.)
- In the Finder, locate any Python program file.
- Get Info on the file, by selecting the file and pressing Command-I.
- In the Get Info window, go to Open With, then Other..., then the CarletonApps folder; choose TextWrangler. You have just declared that you want this particular Python file to open in TextWrangler.
- In the Get Info window, press Change All.... You have just declared that you want all Python files to open in the same way.
When you type on a computer, you use various letters, digits, and symbols. You also use various white space characters, including spaces, tabs, returns, etc. Sometimes one kind of white space can look like another. For example, depending on how your text editor is set up, a tab may look just like four spaces. A line of text may end because of a return, or because of a space between words, if your text editor wraps subsequent words to the next line.
Even though a tab may look like four spaces, or a space may look like a return, the Python interpreter can tell the difference. And Python is rather unusual among programming languages, in that it cares about the difference. When you indent a block of Python code, all of the indentations must match exactly. If they don't, then you get an error. Such errors can be hard to correct, because you can't even see them. 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.
Special symbols are displayed for returns and tabs, while spaces are still invisible. We see that each line ends in a return. Lines 15-21 and 24-25 are indented using spaces. But lines 22-23 are indented using tabs. Python doesn't like that. I fix this by manually changing each tab to four spaces. I save and re-run the program, and it works. I can then turn off Show Invisibles (because it's irritating), or leave it on (for safety).