Lab 1: Welcome to Physics 999

This course will including programming in a language called python. No prior experience with python is necessary.

All of the work you do in python will be in an electronic notebook like this one. It can contain both regular text and computer code.

Each week you will turn in a notebook like this containing your computer code and your discussion of the code.

Goals this week:

By the end of this notebook you should be able to:

  • Rename an ipython notbook.
  • Edit the text in a cell of an ipython notebook and "render" the cell.
  • Edit and run computer code in an ipython notebook.
  • Know what to do in an ipython notebook "emergency".

Rename this notebook

At the top of this notebook it says "IP[y] Notebook"; next to that is the name of the notebook, "Lab 1". Click on "Lab 1" and rename the notebook. The new name should include the words "Lab 1" and your name (assuming you want credit for the lab).

Edit the text in a cell

You can edit the text in any section of a notebook by:

  1. double-clicking on it to start editing
  2. Pushing Shift-Enter when you are done editing ("Shift-Enter" means to push the Enter key while holding down Shift)

You edit this cell... double click on it, edit, then Shift-Enter when you are done editing.

(You may have noticed you can do basic formatting like making text bold...feel free to ignore that for now).

Edit and run computer code

How to run code

Computer code in an ipython notebook looks different thatn normal text. It is in a gray block with special prompts, like In[ ].

You run a code cell the same way you make a text cell look nice: Push Shift-Enter.

Click on the cell below and push Shift-Enter.


In [ ]:
print "Hello stranger!"

How to edit and run code

Editing code is just like editing text:

  1. Double click on a code cell to edit it.
  2. Push Shift-Enter when you are done. This runs code in the cell.

Edit the code in the cell above so that it prints Hello physicist! when you run it (or put you name in in place of stranger).

What to do in an emergency

What is an emergency in a notebook?

There are no real emergencies in a notebook, of course...but there will be times when things don't work as exepcted.

Sometimes that happens because there is an error in your code. Sometimes it happens because notebook cells are run out of order. Sometimes there is a problem behind the scenes with how python is operating.

To see an example of what an error message looks like in a notebook, execute the code cell below; no need to fix the error, the point is simply to see what one looks like.


In [ ]:
print "Goodbye for now

Errors like this are almost always mistakes in the code in the cell you are running, but can sometimes occur for more compicated reasons.

What do I do if there is an error?

Three things:

  1. Try fixing the code in the cell and re-running. If that doesn't working, then...
  2. ...try restarting the ipython "kernel": Go to the "Kernel" menu above, select "Restart" and click "Restaaret" on the dialogue box that comes up. Then re-run all of the cells from the top of the notebook, down. If that doesn't work, then...
  3. Shutdown the notebook entirely and restart.

At times like this it is helpful to remind yourself of something I read once in a programming book:

While learning something new, many students will think, “Damn, this is hard for me. I wonder if I am stupid...” Before going any farther, assure yourself that you are not stupid and that some things are hard. Armed with this silly affirmation and a well-rested mind, you are ready to [learn anything].

Aaron Hillegass, Cocoa Programming for Mac OSX, 3rd Ed.