Skip to course content
Free course

Python Foundations / Module 1 / Notebook Cells and Kernels

Module 1 lesson

Notebook Cells and Kernels

Unit ID: M01-U02 Estimated active time: 18-25 minutes

A notebook is an interactive document

A Python notebook can contain different kinds of cells. We will mainly use:

  • Markdown cells for headings, explanations, questions, and notes.
  • Code cells for Python instructions and their output.

The code runs in a kernel. You can think of the kernel as the active Python process connected to the notebook. It remembers values created during the session until you restart or shut it down.

Run one cell

Create or select a code cell containing:

print("The kernel is ready")

Run the cell. The interface should show that the cell is busy and then display:

The kernel is ready

Notebook interfaces differ, but they normally provide controls to run a cell, interrupt running code, restart the kernel, and run all cells.

Execution counts tell a story

Many notebook interfaces place a number beside a cell after it runs. The number records the order in which code was executed, not the visual position of the cell.

A cell near the bottom can have execution count 1. A cell at the top can have count 5. That means the bottom cell ran first and the top cell ran later.

This matters because the kernel follows execution order. It does not assume that you ran the page from top to bottom.

The kernel remembers

Run this cell:

message = "Python remembers this value"

No output is required. The name message now refers to the text value.

Run a second cell:

print(message)

The kernel can use the value because the first cell already created it.

Now restart the kernel and run only the second cell. You should see a NameError because the new kernel does not know message yet.

This is not a damaged notebook. It is evidence that the second cell depends on the first.

Interrupt and restart are different

Interrupt asks the kernel to stop the code that is currently running. Existing remembered values may remain.

Restart ends the current kernel process and starts a fresh one. Remembered values are removed.

You may need to interrupt code that runs for too long. You use restart when you want to test whether the notebook works from a clean state.

Guided practice

  1. Run a Markdown cell and notice that it formats text instead of executing Python.
  2. Run a code cell that calculates 9 * 9.
  3. Create course_name = "Python Foundations" in one cell.
  4. Print course_name in a second cell.
  5. Restart the kernel.
  6. Run the print cell first and inspect the error.
  7. Run the creation cell, then rerun the print cell.

Check your understanding

After a kernel restart, which statement is correct?

  • All previously created Python values are still remembered.
  • The notebook file remains, but the new kernel must execute the required cells again.
  • Every code cell is automatically deleted.

The second statement is correct.

Takeaway

Cells organise the document. The kernel executes code and holds the current session state. Execution order, interruption, and restart are practical tools, not advanced features. Next, we will use them to find hidden state.