Introduction to the jupyter notebook

**Authors**: Thierry D.G.A Mondeel, Stefania Astrologo, Ewelina Weglarz-Tomczak & Hans V. Westerhoff
University of Amsterdam
2017

This material is heavily based on Learning IPython for Interactive Computing and Data Visualization, second edition.

This first notebook is a fast introduction to the user interface of the Jupyter notebook. The point is to get you comfortable with executing cells of code, adding new ones and finding your way around.

Do not focus too much on understanding the python code in this document. This is just an introduction to the user interface.

The Jupyter notebook

Jupyter notebook, formerly known as the IPython notebook, is a flexible tool that helps you create readable analyses, as you can keep code, images, comments, formulae and plots together.

For reference and if you are interested

Take the tour

**Assignment (5 min):** Take the user interface tour by clicking help > User Interface Tour

Cell menu

The "Cell" menu has a number of menu items for running code in different ways. These include:

  • Run and Select Below
  • Run and Insert Below
  • Run All
  • Run All Above
  • Run All Below

Edit menu

The "Edit" menu has some common options to manage your cells. These include:

  • splitting and merging cells
  • Find and replace
  • Moving cells up and down (also notice the arrow keys in the toolbar)
  • image inserting

Fancy menu interaction

Another way to access keyboard shortcuts, and a handy way to learn them is to use the command palette: Cmd + Shift + P (or Ctrl + Shift + P on Linux and Windows). This dialog box helps you run any command by name - useful if you don’t know the keyboard shortcut for an action or if what you want to do does not have a keyboard shortcut. Once you start using it you’ll wonder how you lived without it!

A notebook has two types of cells

  • A Markdown cell contains text. In addition to classic formatting options like bold or italics, we can add links, images, HTML elements, LaTeX mathematical equations, and more.
  • A code cell contains code to be executed by the kernel. The programming language corresponds to the kernel's language. We will only use Python here, but you can use many other languages.

**Assignment (2 min):**Try adding a code cell and a markdown cell below.

In the toolbar use the "+" the add new cells. Focus on one cell and use the toolbar to make it a code or markdown cell.

  • In the code cell try computing 2*2
  • Write some text, e.g. your name, in the markdown cell

Keyboard shortcuts

If you are on a Mac replace ctrl by cmd.

  • Ctrl-Enter: run the cell
  • Shift-Enter: run the cell and select the cell below
  • Alt-Enter: run the cell and insert a new cell below
  • Ctrl-s: save the notebook

**Assignment (1 min):** Try these out on the cells above

Running Code

First and foremost, the Jupyter Notebook is an interactive environment for writing and running code. The notebook is capable of running code in a wide range of languages. However, each notebook is associated with a single kernel. This notebook is associated with the Python kernel, therefore runs Python code.

Code cells allow you to enter and run code

Run a code cell using Shift-Enter or pressing the button in the toolbar above:


In [ ]:
a = 10
a

Managing the Kernel

Code is run in a separate process called the Kernel. The Kernel can be interrupted or restarted.

**Assignment (1 min):** Try running the following cell, it contains a sleep command that will do absolutely nothing for 20 seconds. During this time the kernal will be busy. Notice that in the top-right corner the circle will be black to indicate this. Hit the button in the toolbar above to interrupt the computation.


In [ ]:
import time
time.sleep(20)

Restarting the kernels manually

The kernel maintains the state of a notebook's computations. You can reset this state by restarting the kernel. This is done by clicking on the in the toolbar above.

Note: Restarting the kernel whipes the memory clean.

**Assignment (2 min):**

  1. Execute the cell below. This will save the variable dna_seq, containing a short string of DNA sequence, in the memory.
  2. Now restart the kernel manually as described above.
  3. In the second cell below try showing the contents of dna_seq.

Does python still know what dna_seq is?


In [ ]:
dna_seq = 'ACCGTCAAA'
dna_seq

Restart the server using the button at the top of the notebook.


In [ ]:
dna_seq

Large outputs

To better handle large outputs, the output area can be collapsed. Run the following cell and then single- or double- click on the active area to the left of the output:


In [ ]:
for i in range(35):
    print(i)

Plots: The power of powers

"The greatest shortcoming of the human race is our inability to understand the exponential function." --Albert Allen Bartlett (https://en.wikipedia.org/wiki/Albert_Allen_Bartlett)

Exponential growth is fast. Consider a population of bacteria or cancer cells. Each generation each bacteria in the population divides in two. The code below shows the (perhaps surprising) rate of growth in the number of bacteria.


In [ ]:
import matplotlib.pyplot as plt

population_size = {0:1} # in generation 0 there is one bacteria
for generation in range(1,25): # simulation of generations 1-24
    population_size[generation] = population_size[generation-1]*2
    
plt.plot(list(population_size.values()))
plt.show()

**Assignment (1 min):** Write down the number of bacteria/cells after 25 generations. Change the number of generations in the cell above to 50 and notice the change on the y-axis.

You doubled the number of generations. By how much did the number of bacteria increase? Are you surprised or not?


In [ ]: