Tutorial 01: Introduction to IPython Notebooks

Python notebooks consist of cells that contain either text (in markdown) or code. You can run the code by highlighting the call and clicking the Run button above. If you have used RMarkdown, IPython notebooks are similar but function slightly differently. One big difference is that a IPython notebook saves the output by default in the notebook itself.

Run a cell

Try to run the code snippet below by clicking on the cell and selecting the Run button above.

In [1]:
1 + 1

You can also run a cell by typing Shift + Enter on the keyboard. This will automatically move to the next cell; doing this is an easy way to run multiple lines together. Test the shortcut here:

In [2]:
1 + 2

In the cells below, write and run code to determine the product of 82 and 91.

In [3]:
82 + 91

You can add new code block using the + sign on the menu (to the left of the Run button). This adds a new code cell below whatever block you are currently on. So, click on this box and add a new code block. Compute the value of 81 divided by 10.

Make your own text box

Now, let's make your own text cell. Click on the empty cell below. Then, select the dropdown menu to the right of Run button and choose the option "Markdown". Then put a message in the cell and Run it. The result should be a new text box.

HERE's some markdown!

Controlling the kernel

You'll notice that ever time you run a cell there is an integer that shows up next to the exectued code. This integer increases each time you run a cell. We can entirely restart the notebook and clear all of the output by selecting Kernel=>"Restart and Clear Output" in the menu. Try this now.

When you save a Jupyter notebook both the code and results are saved. This has benefits and drawbacks (easier to pass results around but much larger files), but does make it easier to submit your results through GitHub. Note though that the Python environment is not saved. So, you'll need to reload datasets and redefine variables. More on this later.