Editing and executing Python programs

For labs in this class, we will use computer programs written in a programming language called Python. This page walks through an example of how to execute and edit a Python program on the machines in our classroom. (You can also download and install Python for your own computer, but the instructions given here may not be exactly correct for your operating system and version of Python.) Now let's work through our example:
  1. Download the Python file hello.py, and save it anywhere you want. These instructions will assume you saved it in the folder Documents|HelloExample.
  2. Open a terminal window (search for "terminal", or find it in the Applications|Utilities folder).
  3. By typing commands in the terminal window, navigate to the folder where you saved your Python program. To do this, you will need to use some or all of the following terminal commands:
  4. To execute the program, enter the terminal command python hello.py. If all goes well, you'll be asked to enter your name and the program will say hello to you.
  5. Sometimes, programs use extra information that is entered when you run them. This extra information is called commandline arguments. For example hello.py can accept a commandline argument that specifies how many times it should say hello. Try this now, by entering the command python hello.py 5. Try it a few times with different values of the commandline argument.
  6. This would be a good time to mention that, in the Terminal window, the up-arrow key brings up your previous command, so you can quickly run it again as is, or change it before rerunning. Try this a few times. Also, pressing the up-arrow key repeatedly cycles through previous commands, which can also be useful. Try it now.
  7. Try running hello.py with a non-numeric commandline argument, such as python hello.py apple. Notice that you receive a mysterious and unhelpful error message, because the program was expecting a number. If you ever receive a mysterious error message during a lab, just ask for help.
  8. Python programs can receive more than one commandline argument. For example, hello.py can accept a commandline argument that specifies your name. Try this now, by entering the command python hello.py 1 Tiberius. Try it a few times with different values of the two commandline arguments.
  9. Now let's try to edit the program and make it do something different. By double-clicking the file hello.py, you can open a type of program called a text editor for editing this file (the name of the text editor we are using is Aquamacs). Do this now.
  10. You don't need to understand the Python code that you see here! If you're interested, you can glance over the code to get some idea of how it works, but this is not necessary.
  11. Let's change the program so that it says 'Goodbye' rather than 'Hello'. To do this, just change the last line of the file by replacing 'Hello' with 'Goodbye'. Now save the file (by choosing "save buffer" in the File menu), and test that your change has worked by running the program from the terminal window again.
  12. Sometimes you will want to interrupt a program that is taking too long to finish. To do this, just press Control-c (i.e. hold down control and press 'c') in the Terminal window. Practice this now, by asking for a ridiculously large number of goodbyes and then interrupting the program while it's running (e.g. python hello.py 10000000).