**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.
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
The "Cell" menu has a number of menu items for running code in different ways. These include:
The "Edit" menu has some common options to manage your cells. These include:
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!
**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.
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.
Run a code cell using Shift-Enter
or pressing the button in the toolbar above:
In [ ]:
a = 10
a
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)
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):**
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
In [ ]:
for i in range(35):
print(i)
"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 [ ]: