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; you can cut and paste from the html version of the book.

Getting Python Running

In a web browser

The easiest way to get up and running with simple examples is to use Python in your web browser, at ideone.come. Click on "python" in the menu under "choose language." (There is "python" for version 2, and "python 3" for version 3. Use version 2.) Type your python program into the box where it says "paste your source code," then click on the Submit button. It will load a new page, and then after a few seconds it will show the output of your program.

After this you typically want to go back and either fix a problem with your program or add more lines to it. To do this, click on the "edit" link in the line that says "source code edit download ShareThis." If you want to start over completely with a new program, just do the same thing (click on "edit"), but then delete your old program from the box.

It's generally not a good idea to use the back button in your browser, click on "home" rather than "edit," or start over by going to the ideone.com address again. If you do this, the web interface forgets that you picked Python as your language, and it also forgets any values that you assigned to variables. For example in the tutorial given in appendix 1 of the book, the example beginning with dwarfs=37 may look totally independent of everything else, but actually the following example, with dwarfs=dwarfs+1, depends on the assumption that 37 has already been assigned to the variable dwarfs.

I've observed that sometimes you get the message "internal error" every time you try to run a program, but then if you wait a few minutes and try again, it works. My interpretation of this is that "internal error" means their server is too busy at the moment.

In many of the real physics examples in the book, e.g., the time1 program in ch. 2, there is a listing of a program that defines a function, and then the book shows the user running the function at the python >>> prompt. For example, the time1 function gets run like this: ">>> time1(1.0,1.0,10)", and this causes it to print the result. The ideone web interface doesn't provide you with a >>> prompt that would allow you to do this kind of thing after your program has been submitted and run, so you would have to put an explicit print statement in your program itself, e.g., "print(time1(1.0,1.0,10))".

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. The rest of the instructions are the same as for Linux, below.

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. Save your file as, e.g., foo.py, in the home directory for the student account (not the desktop). To run it, type "python foo.py" at the operating system's % prompt. This will run the program, and the only output you'll get will be from print statements in the program. To avoid having to type "python foo.py" repeatedly, you can type control-P to bring back the previous command.

Sometimes it's convenient to be able to use the program stored in the file to define some functions, then play around inside the python interpreter to test out your functions. To do this, type python -i foo.py at the operating system's % prompt. Your program executes, and then you have python's >>> prompt. If you then decide to make some changes to the program stored in your file, you can hit control-D to get out of python, and then at the % prompt hit control-P.

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 Start : All Programs : Python 3.1 : IDLE. In the Start menu, you'll have options for several different ways of running Python. IDLE is a fancy programming environment for Python. It's 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 by going to the Run menu and doing Run Module. The examples in the book have print statements like "print 2+2", but starting in version 3 of python you need to use parentheses: "print (2+2)".

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.