Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 39 ยท Getting Started

Writing and Running Your First Notebook

Create your first Jupyter notebook step by step: run code cells, see the last expression displayed, write Markdown, save the file and share a clean result.

Time to make something. In this lesson you build a small notebook that calculates a savings plan: a few code cells, some Markdown explanation, and a tidy final result. You will see how a cell runs, what gets displayed automatically, and how to structure a notebook that others can read.

You need Jupyter on your own machine for the real thing (see Installing and launching Jupyter). The runnable blocks on this page are Python stand-ins: the code inside them is exactly what you would type into a code cell, but the editor here is not a notebook, so the last-expression display is simulated with print.

Step 1: create the notebook

In JupyterLab, click the Notebook button for your Python kernel in the Launcher, or choose File โ†’ New โ†’ Notebook. A file called Untitled.ipynb appears. Rename it through File โ†’ Rename Notebook (right-click the tab or file in the browser) to something meaningful such as savings-plan.ipynb. Use lowercase letters, digits and hyphens: it works well with version control and web links.

The notebook starts with one empty code cell. Click inside it: you are in edit mode.

Step 2: run a code cell

Type this into the first cell:

Python
monthly = 250
months = 36
monthly * months

Press Shift+Enter. The kernel runs the three lines, the prompt changes from [ ] to [1], a new empty cell appears below, and under the first cell you see 9000. Only the value of the last expression is displayed. The first two lines assigned variables, which return nothing to show.

Three ways to run a cell:

Keys Effect
Shift+Enter Run the cell and move to the next one (creating it if needed).
Ctrl+Enter Run the cell and stay on it.
Alt+Enter Run the cell and insert a new cell below.

(On macOS use Cmd or Option in place of Ctrl and Alt as appropriate.)

Here is the same code as a stand-in. In a real notebook the final line would show itself; here we print it:

Python Stand-in: the code you type into the first cell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 3: print versus the last expression

A code cell can produce two kinds of output:

  • Printed text from print() appears as a stream, and every print call counts.
  • The result of the final expression appears after an Out [n]: label, formatted richly (a table for a DataFrame, an image for a figure).

If a cell ends with an assignment there is no Out. If you want to see two values, either print them or end the cell with a tuple. Add a semicolon to hide an output, as in some_call();.

Stand-in that applies the same "show the last expression" rule using Python's own ast module. It is a tiny imitation of what the notebook does for every cell:

Python Stand-in: show only the value of the last expression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 4: add a Markdown cell

Click the first cell, press Esc to enter command mode, then B to add a cell below, then M to turn it into Markdown. Double-click a Markdown cell to edit it. Type:

MARKDOWN
# Savings plan

We save **250** each month for **36** months. The question:
how much is in the account at the end?

- Deposit per month
- Number of months

Run it with Shift+Enter: the cell turns into formatted text with a large heading, bold words and a bullet list. Double-click it to edit again. Markdown cells are where you give the "why" that code cannot express. Cell types and Markdown explains the full syntax, including maths.

Step 5: build a story

A good notebook reads top to bottom as a story:

  1. A title and a short purpose statement.
  2. A cell with imports and settings.
  3. The data or inputs.
  4. The steps, each with a Markdown note before it.
  5. A conclusion that states the result in words.

Here is a small version of the whole story, packaged as a stand-in so you can run it. Each # %% comment marks where a cell would begin (some editors use exactly this notation for notebook-style scripts):

Python Stand-in: the savings notebook as one script

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 6: save and restart cleanly

Press Ctrl+S to save. Before you share the file, do a final Kernel โ†’ Restart Kernel and Run All Cells. If the notebook works from top to bottom on a fresh kernel, then everything it needs is inside the file, and it will work for the next person. If an error appears, you probably depended on a variable defined in a cell you later deleted or ran out of order (see Execution order and hidden state).

Step 7: closing properly

Choose File โ†’ Close and Shut Down Notebook. That ends the kernel and frees memory. Next time, open the file again: the code and the saved outputs are still there, but the variables are not; you must run the cells again to rebuild them.

Name your cells' purpose in a comment on the first line, such as # load data or # plot result. It helps you scan a long notebook.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A code cell ends with the line monthly * months. What appears in the output area?

    The kernel displays the value of the last expression in the cell, labelled Out[n].
  2. 2. Which shortcut runs the cell and moves to the next one?

    Shift+Enter runs the selected cell and selects the cell below, creating one if needed.
  3. 3. How do you hide the automatic output of the last expression?

    A trailing semicolon suppresses the display of the final value in IPython-based kernels.
  4. 4. Why run Restart Kernel and Run All before sharing?

    A clean top-to-bottom run proves no hidden state or out-of-order execution is needed.

Summary

  • Create a notebook from the Launcher or File, New, then give it a meaningful name.
  • Shift+Enter runs a cell and moves on; the last expression's value is displayed automatically.
  • print() output and the final value are two different kinds of output.
  • Use Markdown cells to explain; structure notebooks as a top-to-bottom story.
  • Save, then Restart Kernel and Run All to prove the notebook is reproducible.
  • Variables disappear when the kernel stops, even though saved outputs remain in the file.

Next: Modes and keyboard shortcuts