Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Understanding and execution notebooks

Open In Colab

Learning objectives

After completing this practical, you will be able to:

  • distinguish between Markdown cells and Code cells

  • execute notebook cells in the correct order

  • explain why execution order and kernel state matter

  • restart a kernel and run a notebook from top to bottom safely

  • avoid common mistakes caused by hidden state


Practical storyline

In this practical, you will build a safe and reliable workflow for working with notebooks.

You will learn:

  1. how notebooks are structured

  2. how code is executed

  3. how variables are stored in memory

  4. why execution order matters

This practical establishes habits that are essential for reproducible scientific work.


Part 1 – Understanding notebooks

Notebooks are divided into cells.

There are two main types:

  • Markdown cells → text and explanation

  • Code cells → executable Python


Editing Markdown cells

This cell is written in Markdown.

Tasks

  1. Double-click this cell.

  2. Change some of the words in this sentence: “This is really very important.”

  3. Make one word italic and another bold using * and _ signs.

  4. Run the cell again (Ctrl + Enter).

  5. Using the Markdown guide below, figure out how to render a third word bold and italic.

Markdown reference: https://www.markdownguide.org/basic-syntax/


Part 2 – Executing code cells

Code cells contain Python instructions.
The example below contains Python code.
When you click on it, notice how the cell type changes from Markdown to Code in the dropdown above.

To run a code cell:

  • Click ▶ Run

  • or press Shift + Enter


Example

# The line below prints text to the screen
# Change it so that it prints something more meaningful.

print("Wo sind die Schnapspralinen?")
Wo sind die Prapsschnalinen?

Tasks

  1. Modify the printed text.

  2. Add a comment explaining what the code does.

  3. Run the cell.


Part 3 – Expressions and output

The next example works with numbers.
Before running the cell, predict what will be displayed.

x = 3
y = 4

x - y
x + y

Tasks

  1. Predict what will be displayed.

  2. Run the cell.

  3. Change x and y.

  4. Run it again.



Part 4 – Making output explicit

To display multiple results, use print().

print(x - y)
print(x + y)

Tasks

  1. Modify the numbers.

  2. Add a third print statement.

  3. Observe how explicit output improves clarity.


Part 5 – Variables and memory

A common programming pattern is:

  1. compute something

  2. store it in variables

  3. reuse it

a = x - y
b = x + y

print("The answers to the two calculations are", a, "and", b)

You have now created variables that exist in the notebook’s memory.


Kernel experiment

Restart the kernel of the notebook:

Kernel → Restart (in Colab: Runtime → Restart Session)

Then run only the last cell.

Good scientific practice requires:

  1. Restarting the kernel.

  2. Running all cells from top to bottom.

  3. Checking that the notebook works without manual reordering.

Task

Restart the kernel again.

Then choose:

Kernel → Restart & Run All
or Click ⏩ (Restart the kernel and run all cells) in the taskbar

Confirm that everything works.


Part 6 – Useful notebook actions

Practice the following:

  • Insert new cells via Insert → Cell Above / Below

  • Add a simple calculation.

  • Add a Markdown explanation.

  • Run cells step by step

  • Save your notebook locally

Remember: execution order matters.


Exercises

  1. Modify the first code cell so that it prints something

  2. Change the values of x and y and observe the results

  3. Add a new code cell with a simple calculation

  4. Restart the kernel and run only one later cell.

  5. Add a Markdown cell describing what you did in form of a bullet point list

  6. Save your notebook locally and open it in JupyterLab

%%markdown
### What I did

- Modified the first code cell to print my name.
- Changed the values of `x` and `y` and observed different outputs.
- Added a new calculation using a new variable.
- Restarted the kernel and noticed that variables were no longer defined.
- Learned that execution order matters in notebooks.

6. Saving and opening in JupyterLab

If running locally:

  1. Click File → Save Notebook.

  2. Open a terminal and navigate to your saved notebook

  3. Activate your environment:

%%bash
conda activate sds210
%%bash
jupyter lab
  1. Navigate to your saved notebook file and open it.



## Summary

You have learned to:

- distinguish Markdown and Code cells  
- execute cells interactively  
- understand why execution order matters  
- store and reuse values  

These mechanics may seem simple, but they are foundational.
Reliable execution is the basis for trustworthy analysis.

### What comes next

So far, you have learned **how notebooks run**.
In the next lessson, you will focus on variables and data structures.