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.
Three things happened:
In [ ]:beside each cell becameIn [1]:andIn [2]:, numbered in the order you ran them.- The first cell printed a line, shown right under it.
- The second cell has no
print, yet it showsOut[2]: 135. And it knewpriceandkilos, 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.printshows 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. Useprintwhen you want to see something in the middle. - A last line that makes nothing (like
append, which changes the list and gives backNone) shows nothing.
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.
-
Rice prices
What the Batangas store charges, in pesos per kilo.
- Regular milled rice
- Well-milled rice
-
-
Well-milled costs
7pesos 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.
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.
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.
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
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.
- Jupyter: The Jupyter Notebook format and interface: notebooks, kernels and the notebook format
- JupyterLab: Notebooks: cells, running them, and restarting the kernel
- IPython: The IPython display system and Out: why the last expression is shown, and the Out[n] history
- The Python Standard Library: Built-in Functions, repr(): how a shown value is written, quotes and all