Jupyter Notebook

This notebook was adapted from https://github.com/oesteban/biss2016 and is originally based on https://github.com/jvns/pandas-cookbook.

Jupyter Notebook started as a web application, based on IPython that can run Python code directly in the webbrowser. Now, Jupyter Notebook can handle over 40 programming languages and is the interactive, open source web application to run any scientific code.

How to run a cell

First, we need to explain how to run cells. Try to run the cell below!

In [ ]:
import pandas as pd

print "Hi! This is a cell. Click on it and press the ▶ button above to run it"

Hi! This is a cell. Click on it and press the ▶ button above to run it

You can also run a cell with Ctrl+Enter or Shift+Enter. Experiment a bit with that.

Tab Completion

One of the most useful things about Jupyter Notebook is its tab completion.

Try this: click just after read_csv( in the cell below and press Shift+Tab 4 times, slowly

In [ ]:

After the first time, you should see this:

After the second time:

After the fourth time, a big help box should pop up at the bottom of the screen, with the full documentation for the read_csv function:

I find this amazingly useful. I think of this as "the more confused I am, the more times I should press Shift+Tab".

Okay, let's try tab completion for function names!

In [ ]:

You should see this:

Get Help

There's an additional way on how you can reach the help box shown above after the fourth Shift+Tab press. Instead, you can also use obj? or obj?? to get help or more help for an object.

In [ ]:

Writing code

Writing code in the notebook is pretty normal.

In [ ]:
def print_10_nums():
    for i in range(10):

In [ ]:

0 1 2 3 4 5 6 7 8 9

If you messed something up and want to revert to an older version of a code in a cell, use Ctrl+Z or to go than back Ctrl+Y.

For a full list of all keyboard shortcuts, click on the small keyboard icon in the notebook header or click on Help > Keyboard Shortcuts.

Saving a Notebook

Jupyter Notebooks autosave, so you don't have to worry about losing code too much. At the top of the page you can usually see the current save status:

  • Last Checkpoint: 2 minutes ago (unsaved changes)
  • Last Checkpoint: a few seconds ago (autosaved)

If you want to save a notebook on purpose, either click on File > Save and Checkpoint or press Ctrl+S.

Magic functions

IPython has all kinds of magic functions. Magic functions are prefixed by % or %%, and typically take their arguments without parentheses, quotes or even commas for convenience. Line magics take a single % and cell magics are prefixed with two %%.

Some useful magic functions are:

Magic Name Effect
%env Get, set, or list environment variables
%pdb Control the automatic calling of the pdb interactive debugger
%pylab Load numpy and matplotlib to work interactively
%%debug Activates debugging mode in cell
%%html Render the cell as a block of HTML
%%latex Render the cell as a block of latex
%%sh %%sh script magic
%%time Time execution of a Python statement or expression

You can run %magic to get a list of magic functions or %quickref for a reference sheet.

Example 1: Let's see how long a specific command takes with %time or %%time:

In [ ]:
%time result = sum([x for x in range(10**6)])

CPU times: user 72 ms, sys: 24 ms, total: 96 ms
Wall time: 94.9 ms

Example 2: Let's use %%latex to render a block of latex

In [ ]:
$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$

$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$