Tutorial#

This tutorial will take the reader through an example of using Jupyter notebooks. Jupyter is the interface to the Python programming language used in the first part of this book.

Installation#

  1. Navigate to https://www.anaconda.com/download/success.

  2. Identify and download the version of Python 3 for your operating system (Windows, MacOS, Linux).

  3. Run the installer. I recommend using the default choices during the installation process.

Warning

If you have already used Python it is still recommended that you use the Anaconda distribution. An explanation for this is available in in the further information section of this chapter. If you are using a Chromebook or a Tablet there are websites and applications that you can use instead of Anaconda to follow along in this book. See the further information section of this chapter for information on this.

Starting a Jupyter notebook server#

Open a command line tool:

  1. On Windows search for Anaconda Prompt (it should be available to you after installing Anaconda). See Starting the notebook server on Windows.

  2. On MacOS search for terminal. Starting the notebook server on MacOS

In there type (without the $):

$ jupyter notebook

Press Enter on your keyboard.

Tip

Throughout this book, when there are commands to be typed in a command line they will be prefixed them with a $. Do not type the $.

This will open a new page in your browser. The url bar at the top should have something that looks like: http://localhost:8888/tree. This is the general interface to the Jupyter server. It shows the general file structure on your computer as shown in The Jupyter interface.

../../../_images/main21.png

Fig. 2 Starting the notebook server on MacOS#

../../../_images/main22.png

Fig. 3 Starting the notebook server on Windows#

../../../_images/main23.png

Fig. 4 The Jupyter interface#

Creating a new notebook#

In the top right, click on the New button Creating a new notebook and click on Notebook. This will be followed by a prompt to choose the programming language to use, this is referred to as the kernel: select Python 3. Change the name of the notebook by clicking on “Untitled” and changing the name. You will call it “introduction” as shown in Changing the notebook name.

../../../_images/main24.png

Fig. 5 Creating a new notebook#

Let us change the name of the notebook by clicking on “Untitled” and changing the name. We will call it “introduction”.

../../../_images/main25.png

Fig. 6 Changing the notebook name#

Organising our files#

Open your file browser:

  1. File Explorer on Windows (see Creating a new directory on Windows).

  2. Finder on MacOS (see Creating a new directory on MacOS).

Navigate to where your notebook is (this might not be immediately evident): you should see a introduction.ipynb file. Find a location on your computer where you want to keep the files for this book, using your file browser:

  1. Create a new directory called pfm (short for “Python for Mathematics”);

  2. Inside that directory create a new directory called nbs (short for “Notebooks”);

  3. Move the introduction.ipynb file to this nbs directory.

../../../_images/main26.png

Fig. 7 Creating a new directory on MacOS#

../../../_images/main27.png

Fig. 8 Creating a new directory on Windows#

Writing some basic Python code#

Go back to the Jupyter notebook server (in your browser). Use the interface to navigate to the pfm directory and inside that the nbs directory and open the introduction.ipynb notebook.

../../../_images/main28.png

Fig. 9 Opening a notebook#

In the first available “cell” write the following calculation:

2 + 2

When you have done that click on the Run button (you can also use Shift + Enter as a keyboard shortcut). When you have done that click on the Run button shown in Running code. You can also use Shift + Enter as a keyboard shortcut.

../../../_images/main29.png

Fig. 10 Running code#

2 + 2
4

Figure Running code shows two different things:

  1. The input: which is the instruction to Python to use the mathematical technique of addition to compute 2 + 2.

  2. The output: showing the output that Python has returned as a result of the instruction.

Writing markdown#

One of the reasons for using Jupyter notebooks is that it allows a user to include both code and communication using something called markdown. Create a new cell and change the cell type to Markdown. Now write the following in there:

As well as using Python in Jupyter notebooks we can also write using Markdown.
This allows us to use basic $\LaTeX$ as a way to display mathematics.
For example:

1. $\frac{2}{3}$
2. $\sum_{i=0}^n i$

When you run that it should look like Rendering markdown.

../../../_images/main30.png

Fig. 11 Rendering markdown#

Saving your notebook to a different format#

Click on File and Download As this brings up a number of formats that Jupyter notebooks can be exported to. Some of these might need other tools installed on your computer but a portable option is HTML. Click on HTML (.html). Now use your file browser and open the downloaded file. This will open in your browser a static version of the file you have been working on. This is a helpful way to share your work with someone who might not have Jupyter (or even Python).

Important

This tutorial has:

  • Installed the Anaconda distribution of Python.

  • Started a notebook server.

  • Created a new notebook.

  • Run some Python code.

  • Written some markdown.

  • Saved the notebook to a different format.