Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. T&Cs apply

Python notebooks

An article giving an overview of Python notebooks, in particular Jupyter notebooks.
The Python logo (a blue snake and a yellow snake arranged to make a plus sign), and the Jupyter logo (the word Jupyter between two orange crescents, with three orbiting grey dots)

Before we try some simple neural network and deep learning examples we will introduce some related software tools, Python notebooks (specifically Jupyter) and Colab.

Both run in a web browser (e.g. the one you’re reading this article in), and while Jupyter may require some installation on your local machine, Colab does not. Colab does require an account with Google to use, though you can just use the same account you might have already to access any of its services such as Drive or Gmail.

Jupyter and JupyterLab notebooks

The basic idea behind Jupyter is the use of notebooks. These are interactive documents you view in a web browser that contain a mix of text and computer code. The code used is generally Python, though it is possible to use it with several other programming languages.

The most up to date version of Jupyter is known as JupyterLab, while the original Jupyter is an older version. Both work in a similar way, though JupyterLab has some more advanced features.

The basic structure of Jupyter and JupyterLab notebooks is use of alternating text cells and code cells.

So you might see a text cell a bit like this:

Hello, welcome to this Jupyter notebook. In it, we will add together two numbers and print the result

And then this will be followed by a code cell a bit like this:

a = 2
b = 3
print(a+b)

However, rather than being just for illustration as here, the code is executable. That is, you can adjust and run the code in an interactive way in the code cells, while providing accompanying notes with explanation or discussion in the text cells.

The best way to see how Jupyter notebooks work is to try them out for yourself. At the time of writing the Jupyter website gives the option to try out some demo notebooks in your browser without installing the software (https://jupyter.org/).

If you have Python Anaconda installed however, you probably already have Jupyter and JupyterLab installed. The quickest way to launch them is by opening an Anaconda Prompt window and typing either

jupyter notebook

for the original Jupyter, or:

jupyter-lab

for JupyterLab. All being well should open up a window in your default web browser that gives you the option to either open a new notebook or an existing one on your hard drive.

A screenshot of the JupterLab launch page

If you’re new to Jupyter we’d probably recommend using JupyterLab as it offers all the features of Jupyter, and is perhaps easier to use. You just click on the button with the Python logo below where it says ‘Notebook’ and it will open a new blank notebook.

IPython

Though the core language we’ll use in Jupyter is Python, it’s actually based on an extended version of Python called IPython. This stands for Interactive Python and as the name suggests is used in interactive contexts, such as as interactive notebooks.

For our purposes IPython is pretty much the same as the Python we’ll use elsewere, except for one so-called magic command we’ll use to display images with Matplotlib onscreen. This is the command:

%matplotlib inline

Without it your images won’t display properly. In general if you see some code preceded by the (%) symbol in a jupyter notebook it is a ‘magic’ command.

Beyond the trick to display Matplotlib images, we won’t really use any other magic commands. For an exhaustive list see the IPython documentation, or for a brief overview see the Python Data Science Handbook.

Running a code cell

Once you’ve written some code in a cell, to run it you need to either click the play button near the top of the screen while the cell is selected, or hit shift and enter while your cursor is in the code cell. To enter a code cell just click in it. After you run a cell, if it’s the last cell in the notebook, a new one should automatically be created below the old one, otherwise you’ll just move onto the next cell.

Once you have a few cells, you can select cells and move between them by clicking immediately to the left of them in the margin.

Making text cells

In Jupyter, to create a text cell you can insert a code cell and convert it to a text cell via the menus:

Cell -> Cell Type -> Markdown

While in JupyterLab you can convert it directly by changing the dropdown menu at the top from Code to Markdown.

This article is from the free online

Machine Learning for Image Data

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now