This course will use a tool called Jupyter Notebooks to run your Python code. It works with the same Python code as we’ve used before but it allows interactive execution and can combine your code with blocks of text to explain what you’re doing and embed output such as graphs directly into the page.
The easiest way to get access to Jupyter Notebooks is to install Anaconda which is a piece of software which includes Python along with lots of other tools. It is freely available for Windows, MacOS and Linux. Anaconda can be installed into your home area on your computer so if you are on a work laptop, for example, you will not need any special permissions. Once Anaconda is installed, start “Anaconda Navigator” and press the JupyterLab button on the main screen:
Throughout this course you will likely want to start a new notebook for each section of the course so name them appropriately to make it easier to find them later. To open a Notebook in Anaconda you can go to JupyterLab and there go to File > New > Notebook or click the Python 3 button under Notebook section in the Launcher tab.
Once the notebook is launched, you will see a wide grey box with a blue [ ]: to the left. The grey box is an input cell where you type any Python code you want to run:
# Python code can be written in 'Code' cellsprint("Output appears below when the cell is run")print("To run a cell, press Ctrl-Enter or Shift-Enter with the cursor inside")print("or use the run button (▶) in the toolbar at the top")
Output appears below when the cell is run
To run a cell, press Ctrl-Enter or Shift-Enter with the cursor inside
or use the run button (▶) in the toolbar at the top
In your notebook, type the following in the first cell and then run it with Shift-Enter, you should see the same output:
a =5b =7a + b
12
The cells in a notebook are linked together so a variable defined in one is available in all the cells from that point on so in the second cell you can use the variables a and b:
a - b
-2
Some Python libraries have special integration with Jupyter notebooks and so can display their output directly into the page. For example pandas will format tables of data nicely and matplotlib will embed graphs directly:
If you want to write some text as documentation (like these words here) then you should label the cell as being a Markdown cell. Do that by selecting the cell and going to the dropdown at the top of the page labelled Code and changing it to Markdown.
It is becomming common for people to use Jupyter notebooks as a sort of lab notebook where they document their processes, interspersed with code. This style of working where you give prose and code equal weight is sometimes called literate programming.
Exercise
Take the following code and break it down, chunk by chunk, interspersing it with documentation explaining what each part does using Markdown blocks:
You don’t need to put only one line of code per cell, it makes sense sometimes to group some lines together.
Throughout this course, use the Jupyter Notebook to solve the problems. Follow along with the examples, typing them into your own notebooks and see how they work.
Answer
Your notebook could look similar to:
My first notebook
We first define the price of apples and bananas.
prices = {"apple": 0.40,"banana": 0.50,}
Then we state how many apples and bananas we have in the basket.
my_basket = {"apple": 1,"banana": 6,}
For each fruit in the basket we calculate the price to pay and add it to the total bill.