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.
You might also want to try a new Jupyter environment JupyterLab.
First, we need to explain how to run cells. Try to run the cell below!
import pandas as pd
print("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.
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. Note that if you're using JupyterLab you don't have an additional help box option.
# NBVAL_SKIP
# Use TAB completion for function info
pd.read_csv(
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!
# NBVAL_SKIP
# Use TAB completion to see possible function names
pd.r
You should see this:
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.
pd.read_csv?
Writing code in the notebook is pretty normal.
def print_10_nums():
for i in range(10):
print(i)
print_10_nums()
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
.
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:
If you want to save a notebook on purpose, either click on File > Save and Checkpoint
or press Ctrl+S
.
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.
Let's see how long a specific command takes with %time
or %%time
:
%time result = sum([x for x in range(10**6)])
Let's use %%latex
to render a block of latex
%%latex
$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} \mathrm{d} x$$