Using Jupyter notebooks

The text on this page assumes you opened the page as an interactive Jupyter notebook, on your own computer, or one of the Jupyter web interfaces.

In the Jupyter notebook, this is a cell.

It’s a cell with text - a text cell.

A text cell can have formatting, such as links.

For example, see Notebook basics for another introduction to the notebook interface.

See that page for an introduction to the notebook Dashboard, where you can open notebooks from a file listing, or create new ones.

If you are in the classic Jupyter Notebook interface (rather than reading this in the textbook), you will see the Jupyter Menu near the top of the page, with headings “File”, “Edit” and so on.

Underneath that, by default, you may see a row of icons - the “Toolbar”.

In the toolbar, you may see icons to run the current cell, among others.

Here’s a picture, for those of you reading this in textbook form.

To move from one cell to the next, you can click the run icon in the toolbar, but it is more efficient to press the Shift key, and press Enter (with Shift still held down). We will write this as Shift-Enter.

This is another text cell.

The next cell is a code cell.

The code cell is for Python code (actually, you can type code in other languages too, but we won’t be using that feature).

Here we will be using Python like a calculator.

Our problem is the following:

Alex and Billie are at a restaurant, getting ready to order. They don’t have much money, so they are calculating their expected bill before they order.

Alex is thinking of having the fish for £10.50, and Billie is leaning towards the chicken, at £9.25. First they calculate their combined bill.

Press Shift-Enter in the cell below, to see the total:

10.50 + 9.25
19.75

The cell above is Python code. As you would predict, Python understands numbers like 10.50, and it understands + between the numbers as an instruction to add the numbers.

When you press Shift-Enter, Python makes its own representation of 10.50, and its own representation of 9.25, and then runs the addition operation on these two, which results in Python’s representation of 19.75.

Finally, Python sends back the 19.75. The notebook detects that Python sent back a value, and shows it to us.

This is exactly what a calculator would do.

Unlike a calculator, we can also put notes next to our calculations, to remind us what they are for. One way of doing this is to use a “comment”.

A comment is some text that the computer will ignore. In Python, you can make a comment by starting a line with the # (hash) character. For example, the next cell is a code cell, but when you run it, it does not show any result. In this case, that is because the computer sees the # at the beginning of the line, and then ignores the rest.

# This bit of text is for me to read, and the computer to ignore.

Many of the code cells you see will have comments in them, to explain what the code is doing.

Practice writing comments for your own code. It is a very good habit to get into. You will find that experienced programmers write many comments on their code.

Let’s continue with Alex and Billie’s struggle with their bill.

They realize that they will also need to pay a tip.

They think a 15% tip is reasonable. Now they need to multiply their total bill by 0.15, to get the tip. Given the bill is about £20, the tip will be about £3.

In Python, as for your calculator, * means multiplication.

What about this?

# The tip - with a mistake.
10.50 + 9.25 * 0.15
11.8875

Oops, that isn’t doing the right calculation.

Python follows the normal rules of multiplication before addition.

See https://en.wikipedia.org/wiki/Order_of_operations.

In the case above, that means Python first calculates 9.25 * 0.15 (to get 1.3875) and then adds the result to 10.50, to get 11.8875.

We need to tell Python to do the addition and then the multiplication. We do this with round brackets (parentheses):

# The bill plus tip - mistake fixed.
(10.50 + 9.25) * 0.15
2.9625

Now your turn. Calculate Alex and Billie’s bill including the tip:

# The bill, including the tip
# Type your code below.

Alex and Billie are each going to pay half the bill. Calculate half the bill (where the bill is the total bill including the tip):

# Half the total bill, including the tip
# Type your code below.

Alex is planning to drink three cherry colas, each at £1.50. Billie is going to have a glass of wine, at £3.50. Now what is their combined bill, plus tip?

# The total bill, including drinks, and the tip.
# Type your code below.