Notebooks and your toolkit · lesson 2 of 6
Parts in this lesson
AIn [1]:the order cells ran in
BOut[1]:the last line's value
C# Markdownnotes between the code
Dkernelthe memory cells share

Your first notebook

Last updated

A notebook is a page of cells: code you run one piece at a time, with each result right underneath, and notes in between. In this lesson you'll run cells, see how a notebook shows results, write a Markdown note, and find out what happens when cells run out of order.

By the end of this lesson you can

  • Run code cells and read their output and Out[n] result
  • Explain the difference between what a cell prints and what it shows
  • Write a Markdown cell with a heading, a list and bold text
  • Use the In [n] numbers and Restart to catch cells run out of order

Cells and their output

In the Python course, a program is one file that runs from top to bottom in one go. A notebook splits code into cells that you run one at a time. That suits data work: load the data once, then try one thing, look at the result, and decide what to try next.

Click into the first cell below and press Run (or press Shift and Enter together, which runs the cell and moves to the next one, as in Jupyter). Then run the second cell.

first.ipynb

Three things happened:

  1. In [ ]: beside each cell became In [1]: and In [2]:, numbered in the order you ran them.
  2. The first cell printed a line, shown right under it.
  3. The second cell has no print, yet it shows Out[2]: 135. And it knew price and kilos, which were made in a different cell.

The next steps explain the last two.

Printed, or shown?

When a cell's last line is a value (a number, some text, a calculation), the notebook shows that value as Out[n], without you asking. It's a shortcut for looking at things, and you'll use it all the time to peek at data.

It isn't the same as print. Run each cell below and compare:

  • "sari-sari" alone is shown with its quotes, because the notebook shows values the way you'd type them in Python. print shows the text itself, without quotes.
  • Only the last line is shown. In the third cell, store.upper() makes 'BATANGAS', but nothing shows it, because it isn't the last line. Use print when you want to see something in the middle.
  • A last line that makes nothing (like append, which changes the list and gives back None) shows nothing.
shown.ipynb

Markdown cells

Notes go in Markdown cells, between the code. Markdown is plain text with a few marks for formatting: # at the start of a line for a heading, **two stars** for bold, - for a list, and backticks for code.

Run all to see the notes formatted. Then click Edit note on the first one, change the heading, and press Show it. Try adding a Markdown cell of your own with Add Markdown below.

prices.ipynb
  1. Rice prices

    What the Batangas store charges, in pesos per kilo.

    • Regular milled rice
    • Well-milled rice
  2. Well-milled costs 7 pesos more per kilo.

Good notebooks read like a short report: a heading saying what the notebook is for, a sentence before each piece of code saying what it's about to do, and a sentence after saying what it found. You'll practise this in lesson 1.3.

One memory, in the order cells run

Behind every notebook is a kernel: a Python that stays running and remembers every name your cells make. That's why a cell can use price made by another cell. But the kernel only knows the order cells ran in, not the order they're written on the page. Step through what that means.

Three code cells with their run numbers blank, blank, blank. Memory holds nothing.In [ ]:price = 45In [ ]:price = 50In [ ]:price * 3Memory (the kernel)empty
1 of 6 Three cells, top to bottom. None has run, so the memory on the right is empty.

Try it: run them out of order

This notebook shows its memory on the right. Run the cells in this order: the first, then the last, then the middle one. Watch the memory and the In [n] numbers.

order.ipynb

The page now says price = 50 and shows 150… or does it? If you ran them first, last, middle, the last cell still shows 135, left over from when price was 45. Now press Restart and run all: memory is emptied, every cell runs from the top, and the numbers go 1, 2, 3.

Restart and run all before you trust a notebook, and always before you share one. If it still works, the page means what it says.

Wrong way: a name that isn't there after a restart

The opposite happens too. Run both cells below, in order: they work. Now press Restart, which empties memory, and run only the second cell.

stores.ipynb

NameError: after a restart, stores doesn't exist until the cell that makes it runs again. The same thing happens when you open a notebook tomorrow: the page still shows yesterday's results, but memory starts empty. Run the cells from the top (or press Run all) and it works again.

Check what you learned

A cell's last line is len(stores), and stores has 5 items. What appears under the cell?
Three cells, top to bottom, show In [1], In [3], In [2]. What does that tell you?
Which Markdown makes a heading?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis