Jupyter notebooks are a way to program interactively, incorporating text, links, math, and graphics straight into the notebook. The gold standard for a notebook is that anyone could download your notebook off the internet and run it to reproduce an analysis you report in a publication.
Jupyter notebooks are built from "cells." There are two basic types of cells in jupyter. Markdown cells (like this one) that can hold text and code cells (like the next one) that hold code.
In [ ]:
print("This is a code cell")
print(5*5)
Shift+Enter
Shift+Enter
.Code
or Markdown.
Insert->Cell
on the menu. Each code cell contains python code. It can be run indepedently of others. To run it, click on the cell and hit Shift+Enter
. To run all of the cells in order, go to the menu and select Cell->Run All
.
When you run a cell, all other cells "know" you ran the code. Try running the three code cells that follow this one.
x = 5
cell.print(x)
cell (it should spit out 5
).x = 7
cell.print(x)
cell (it should now spit out 7
). What matters is what cell ran last, not where the cell is up and down on the page. Because x = 7
was the last code ran, print(x)
will now return 7
wherever that command is in the notebook.
In [ ]:
x = 5
In [ ]:
print(x)
In [ ]:
x = 7
Kernel->Interrupt
. Kernel->Restart
. You'll have to rerun all cells at this point.Lots of code needs to dump out a graph. This can be achieved by putting the "magic" directive %matplotlib inline
in a code cell (this usually goes at the very top of the notebook). After that is called, all graphs will be captured by the notebook. Run the following to cells to see how it works.
In [ ]:
%matplotlib inline
In [ ]:
from matplotlib import pyplot as plt
plt.plot([1,2,3],[4,5,6])
This is the place you should take notes describing what you're doing and why. Markdown let you annotate your science with pretty formatting. You write text normally, but then add a few simple markdown flags to make the cell pretty.
x = 2
.# This is a multi-line code block. It won't run, but it will look nice
x = 2
print(x*x)
# See?
Tables | Are | Cool |
---|---|---|
a | $x=7$ | 1600 |
b | print(x) |
12 |
Like adding color.
In [ ]: