Jupyter Notebooks

Overview:

  • Teaching: 10 min
  • Exercises: 10 min

Questions

  • What is a Jupyter notebook?
  • Why should I use one?

Objectives

  • Create a Jupyter notebook
  • Enter code into the notebook
  • Augment the code with a description in markdown

You should have a Python 3 notebook open, if not take a look at the setup page (the previous page in the schedule).

Trying things out

In this lesson you can have a Python 3 jupyter notebook open to try out any of the commands you see here and reproduce the results.

Python in notebooks

Jupyter provides a nice web-based interface to Python. In the below cells you can type a line of Python. For example, here we type in

a = 5

When we press SHIFT+Return this Python line is interpreted interactively

In [1]:
a = 5

To see that this has worked, let's print the value of a

In [2]:
print(a)
5

You can type whatever Python you want, and it will be evaluated interactively. For example...

In [3]:
b = 10
In [4]:
print(a + b)
15

One of the cool things about a Jupyter Python notebook is that you can edit the above cells and re-execute them. For example, change the value of b above and re-execute the lines [3] and [4] (by selecting the cell and pressing SHIFT+Return).

Out of order execution

The ability to go back and change only small snippets of code is very useful, but also very dangerous from a coding point of view. If you edit a code cell and don't run all the code cells after it, then any cell that isn't re-executed is still using the old code. Jupyter allows you to keep track of this by numbering its input, In [3] for instance means this block was executed third.

If you get in a complete mess you can also clear all outputs, without removing the input and re-execute the code blocks in order. There is a drop-down menu under the Kernel option, in the toolbar at the top of the notebook for this.

For your learning we would recommend during this course that you never go back and change anything. Just re-write the code in the next cell. This way you will have a record of everything you have done, examples of common mistakes which you can refer back to in order to reinforce your learning.

Hello Notebook!

Use the cells in your notebook to type three lines of Python:

a = "Hello"
b = "Notebook!"
print(a,b)

Execute the cells. This should print out Hello Notebook!.

Now go back and change the values of a and b to Goodbye and Everyone. Re-execute your cells. This should now print out Goodbye Everyone.

Solution

Markdown in notebooks

Another useful feature is that you can mix documentation into your notebook. Do this by selecting a cell from the menu bar and using the dropdown menu (on the toolbar at the top) to change the cell type to markdown. You can now add documentation, using markdown formatting. For example, use the hash symbol for headers.

Typing

# This is a big header

## This is a subheading

### This is a sub-sub-heading

and pressing Shift + Return results in:

This is a big header

This is a subheading

This is a sub-sub-heading

You can add in hyperlinks using square brackets and round brackets. For example

[link to github](https://github.com)

produces:

link to github

Mastering markdown

Now change the type of the cell in your notebook to markdown. Type in some markdown in the cell and experiment with adding in headings and hyperlinks. Take a look through the markdown cheat sheet and see if you can add bullet point lists, images, or code blocks to your cell.

Solution

Key Points:

  • Jupyter notebooks allow you to write any Python code into a web interface.
  • Cell contents can be easily modified.
  • You need to be wary of out of order execution.
  • Markdown is useful for explaining what the code does and presenting your work.