courses > physics 221-223 > Python

To get started with Python, I suggest you first try out the examples in Appendix 1 of the book, then try the physics examples in section 2.2. A good way to see if you really understand what's going on would be to think of things you could do that would involve changing the programs in the book a little bit. It's not necessary to type in every line of every program if you don't want to; the book's web page has a file you can download that contains all the programs in the book -- click on the link that says "code listings."

Getting Python Running

On a mac

Python is installed by default on MacOS X. To use it, go to the Applications folder, then go into Utilities, and run the Terminal application. At the % prompt, type "python".

On the Linux machines in the classroom

The username and password for these machines are posted on the monitors. In the Applications menu, find the Accessories submenu, and select Terminal. At the $ prompt, type "python".

Once you get into the more complicated examples, like the ones that define functions, it gets to be a pain to type in your examples directly at the python prompt. You can open a text editor (similar to NotePad on Windows) by doing Applications:Accesories:Text Editor. Then if your program is called, e.g., foo.py, you can run it by typing python -i foo.py.

As you get into even more complicated examples, like the programs you'll write for some of the homework problems, it's easiest just to start by cutting and pasting an example from the book and then modifying it for your purposes. Some useful examples can be cut and pasted directly from the online version of section 2.2. You can also download a zip file of all the code listings in the book.

On other Linux machines

Almost all Linux machines have python installed by default. The procedure for opening a terminal window may be slightly different, depending on what window manager you use; just search for it in the menus.

On Windows

Go to python.org/download, download the installer for the Windows version, and double-click on the installer icon on the desktop. Use all the defaults in the install wizard, except that if you're on one of the Windows machines in the classroom, you should pick "Install just for me." In the Start menu, you'll have options for several different ways of running Python. The most basic one is so basic that it doesn't have any way of cutting or pasting, or saving a program that you've written. The one you want is under Start - All Programs - Python 2.6 - IDLE (Python GUI). IDLE is fancy, and is documented here, but you don't really need the documentation in order to do basic things we're doing; just edit your programs using NotePad, open them from the File menu in IDLE, and run them.

On a Windows machine running a Linux live CD

I have a stack of CDs in a cupboard at the back of the classroom. These are "live" Linux CDs, meaning that you can stick them into a Windows computer, restart the computer, and the computer will automatically run Linux off of the CD. While it's starting up Linux, it offers you various opportunities to change the defaults, but if you simply sit back and wait without touching the mouse or the keyboard, it will just keep on chugging away and eventually start up in Linux. Running the computer from the CD is slow at first, because it has to read a bunch of stuff off of the CD, but once it gets going, it's just as fast as running a normal operating system from the hard disk. Wait a few minutes for it to finish starting up in linux.

Next we need to install the gedit editor, which isn't installed by default when you run the live CD. In the Applications menu, find the Accessories submenu, and select Terminal. At the $ prompt, type "gedit," and it will tell you that gedit isn't installed, but it will tell you that you can install gedit by doing the command "sudo apt-get install gedit." You can cut and paste this command rather than having to type it in.

Finally follow the directions above for running python on Linux.

On your own Windows machine at home

Go to python.org. Click on the link that says Windows Installer. Save the file to your desktop. On the desktop, double-click on the file, which will have a name like python-2.5.2.msi. Click through all the steps on the install wizard.