Jupyter/IPython Introduction

This notebook will give you some pointers on how to use Jupyter. All of our exercises are conducted using Jupyter, so spending a few minutes to get to know the tool will help you later on.

Use the Help

The very first thing to explore is the Help menu. Notice it above, in the notebook menubar? It is just to the right of the word Kernel. Click on it to reveal the options.

Once you've revealed the menu, choose the option called User Interface Tour. Jupyter will take you on a short, guided tour of the user interface you are currently using. This will probably only take you a few minutes, but it is super helpful.

Two other options on the Help menu are worth pointing out. First is a link to the complete Jupyter documentation, listed as Jupyter Help. Second is a quick cheat-sheet of keyboard shortcuts, listed as Keyboard Shortcuts. Spend a little time here, and you will be able to navigate around the tool without ever touching the mouse. Keyboard shortcuts are the way true programmers move like ninjas around their programs.

Structure of a Notebook

An Jupyter notebook is made up of cells. Within a notebook, one of the cells is active. The active cell has an extra box around it. To make a cell active, you can select it with your mouse, or use your arrow keys to move to the appropriate cell.

Notice how as you click around from cell to cell, the cell type displayed in the notebook toolbar changes from things like Markdown to Code to different levels of headings.

Some cells contain written text like this. These are called Markdown cells. Markdown is a simple way of formatting text.

Other cells contain Python code. These are called Code cells, and you can tell them apart from other cells because they have words like In [ ] to the left. This indicates that the cell is input to Jupyter, and it can be run. If the cell has been run, the brackets will contain a number showing the step when the code was run. The first cell to be run will be given the number 1, the second will be given the number 2, and so on. If you re-run the same cell over again, its step number will be updated to show the latest number. If the code in the cell takes a while to run, the brackets will be filled with an asterisk (' * ') to show you that it is still going.

Some code cells produce output. For example, if they use the print command. If the cell produces output, it will appear below the cell. Other code cells contain expressions that return results. When an expression returns a result, Jupyter will show it below the code cell and it will have words like Out [ ] to the left of it.

You can execute the code in the cell by pressing Ctrl-Enter (the Control or Option key, plus Enter). Alternatively, you can use Cell->Run from the notebook menubar.

Try running the cell below.


In [ ]:
# Example of a Code cell that prints output
print "Hello World!"

In [ ]:
# Example of a Code cell that contains an expression
5 + 6

The REPL Environment

One of the key features of the Jupyter environment is that it supports what's called a REPL environment:

  • R for Read
  • E for Evaluate
  • P for Print
  • L for Loop

What this means is that when you execute a code block (pressing the "play" button), Jupyter reads what you've entered, tries to evaluate it and run it as Python code, prints the results for you to see, and loops, or waits for you to do the same thing again.

The key thing is that each cycle through the REPL loop happens in the same Python environment, so things you set or changed in one REPL loop are still available the next time you execute it. So if you set a variable, or import some libraries, they are still available in the next loop.

This makes it easy to experiment with code. You can run it, see what happens, change it, run it again, add onto it to make it do more stuff, etc.

Many experienced programmers build programs piece-by-piece. They start with something very simple, and keep adding onto it, one bit at a time, until they are done. The REPL environment is a great way to do this.