Functions and Classes in Python

Jupyter Python Notebooks

Overview:

  • Teaching: 10 min
  • Exercises: 5 min

Questions

  • What are Jupyter Notebooks?
  • Why might I want to use them?

Objectives

  • Know how to enter Python within a notebook
  • Understand the difference between code and markdown cells
  • Know that you can add documentation/description to your code with markdown

Execute code in a notebook

Jupyter provides a nice web-based interface to Python. In a new notebook 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

Info: Out of order execution

The ability to go back and change only small snippets of code is very useful, but also very dangerous form 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 output, without removing the input and re-execute the code blocks in order.

All of the standard Python help is available from within the notebook. This means that you can type

help(something)

to get help about something. For example, type and execute help(print) to get help about the print function...

In [5]:
help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

Hello Notebook!

Use the cells in your notebook to type into the three Python lines

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.

Save your work

You can set the name of the notebook by clicking on the name at the top, and entering the new name e.g episode1 (The file extension is ipynb which means 'interactive python notebook'). You can save the notebook on your local machine using the File menu at the top, using Download as e.g. saving as a normal Python script, or as a PDF file, webpage or downloading the python notebook itself.

You can run this notebook on your own computer by installing Jupyter. More information is available at the Jupyter website.

Using Markdown to document your code

Another cool thing is that you can mix documentation into your notebook. Do this by selecting a cell and using the dropdown menu to change the cell type to Markdown.

You can now add documentation, using markdown formatting. For example, use the hash symbol for headers...

NB In what follows you do not need to include the line %% markdown, this some magic we use to generate the teaching material. More on magic later ...

In [6]:
%%markdown
# This is a big header

## This is a subheading

### This is a sub-sub-heading

This is a big header

This is a subheading

This is a sub-sub-heading

In [7]:
%%markdown
[link to github](https://github.com)

Markdown

Insert a new cell and change it to Markdown. Type in some markdown in this 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.

See if you can recreate the following:

A super subheading

  • list item 1
  • list item 2
  1. numbered list item
  2. another numbered list item
  3. another item

Image from the web

# block of code with python syntax highlighting
for k in range(10):
    print(k, 'Hello World!')

Solution

Help!

Finally, use the interactive Python help to get help about the open function for reading and writing files.

Solution

Key Points:

  • Jupyter notebooks allow you to write any Python code (and other languages) 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.