Tutorial#

Installation#

  1. Navigate to https://www.anaconda.com/products/individual.

  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 later.

Starting a Jupyter notebook server#

We are going to use Jupyter notebooks for the first part of this book. This interface to Python works inside your web browser but does not require an internet connect.

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. See Starting the notebook server on MacOS.

In there type:

$ jupyter notebook

and then press Enter on your keyboard.

Tip

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

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

Fig. 2 Starting the notebook server on MacOS#

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

Fig. 3 Starting the notebook server on Windows#

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.

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

Fig. 4 The Jupyter interface#

This is the general interface to the Jupyter server. It shows the general file structure on your computer.

Creating a new notebook#

In the top right, click on the new button and click on Python 3.

../../../_images/main27.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/main28.png

Fig. 6 Changing the notebook name#

Once this is done let us close the notebook by closing the corresponding tab of your web browser.

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.

Let us rearrange things.

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 cfm (short for “Computer 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/main29.png

Fig. 7 Creating a new directory on MacOS#

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

Fig. 8 Creating a new directory on Windows#

Writing some basic Python code#

Go back to our Jupyter notebook server (in your browser).

Use the interface to navigate to the cfm directory and inside that the nbs directory and open the introduction.ipynb notebook.

../../../_images/main31.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).

../../../_images/main32.png

Fig. 10 Running code#

2 + 2
4

We see two different things there:

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

  2. The output: Out [1] 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 us 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:

../../../_images/main33.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

In this tutorial we have

  • Installed the Anaconda distribution of Python.

  • Started a notebook server.

  • Created a new notebook.

  • Run some Python code.

  • Written some markdown.

  • Saved our notebook to a different format.