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:
how notebooks are structured
how code is executed
how variables are stored in memory
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¶
Double-click this cell.
Change some of the words in this sentence: “This is really very important.”
Make one word italic and another bold using
*and_signs.Run the cell again (
Ctrl + Enter).Using the Markdown guide below, figure out how to render a third word bold and italic.
Markdown reference:
https://
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.
# 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¶
Modify the printed text.
Add a comment explaining what the code does.
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 + yTasks¶
Predict what will be displayed.
Run the cell.
Change
xandy.Run it again.
Part 4 – Making output explicit¶
To display multiple results, use print().
print(x - y)
print(x + y)Tasks¶
Modify the numbers.
Add a third print statement.
Observe how explicit output improves clarity.
Part 5 – Variables and memory¶
A common programming pattern is:
compute something
store it in variables
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.
What happens?
You should see an error such as:
NameError: name 'x' is not definedWhy?
Good scientific practice requires:
Restarting the kernel.
Running all cells from top to bottom.
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¶
Modify the first code cell so that it prints something
Change the values of
xandyand observe the resultsAdd a new code cell with a simple calculation
Restart the kernel and run only one later cell.
Add a Markdown cell describing what you did in form of a bullet point list
Save your notebook locally and open it in JupyterLab
Sample solution (click to expand)
1. Printing something¶
You could modify the first code cell like this:
print("Es jibt eben sone und solche, und dann jibt es noch janz andre, aba dit sind die Schlimmstn, wa?")(Use your own words instead.)
2. Changing x and y¶
Example:
x = 10
y = 2
x - y
x + yOutput:
12Only the last expression (x + y) is shown automatically.
3. Adding a new calculation¶
Example:
z = 6
result = z * 3
print("z times 3 is", result)This creates a new variable and prints the result explicitly.
4. Restarting the kernel and running only one later cell¶
If you restart the kernel and run only:
a = x - y
print(a)You will likely get an error like:
NameError: name 'x' is not definedReason:
Restarting the kernel clears all variables from memory.
Since
xandywere defined in earlier cells, they no longer exist.
This demonstrates how notebooks depend on execution order.
5. Adding a Markdown explanation¶
Example Markdown cell:
%%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:
Click File → Save Notebook.
Open a terminal and navigate to your saved notebook
%%bash
conda activate sds210%%bash
jupyter labNavigate 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.