Notebooks
Literate Programming
In 1984, Donald Knuth pioneered literate programming, shifting the focus from telling a computer what to do to explaining the "why" and "how" to humans. After all, while computers only care about logic, humans also prioritize clarity; code is ultimately read by people far more often than it is executed by machines. A brilliant program that only a computer can decipher has limited value.
Notebooks are a modern implementation of this idea, combining narrative text with live code to make work easy to understand, share, and revisit.
Creating a Notebook
Prerequisite: Anaconda is installed and JupyterLab is running. Refer to the previous lesson for details.
Start your first notebook by clicking the Python 3 button under the Notebook section in the Launcher.

By default, this creates a file named Untitled.ipynb in your current folder. Right-click the file and select Rename to change it to firstConcept.ipynb.

What is an .ipynb file?
The .ipynb extension denotes a Jupyter Notebook document. It is a plain JSON file containing code blocks, execution outputs, narratives (Markdown), and images. These files can be exported to formats like HTML or PDF for easy sharing.

Interactive Computing
Try using the code cell as a calculator:
20 + 30
Press Ctrl + Enter to run the cell. The output will appear directly below:
50
Execution Indicators:
- In [x]: The number x indicates the execution order.
- In [*]: An asterisk indicates that the cell is currently running.
You can add multiple cell types in JupyterLab:
- Code: For executable Python code.
- Markdown: For formatted text and narratives.
- Raw: For text that should not be evaluated by the Python Kernel.
Keyboard Shortcuts
Using keyboard shortcuts significantly improves efficiency. Click to the left of a cell to enter "Command Mode" (the cell border will turn blue) and use the following shortcuts:

Common Keyboard Shortcuts
| Shortcut | Result |
|---|---|
a |
Add a code cell above the current cell |
b |
Add a code cell below the current cell |
dd |
Delete the current cell |
m |
Convert the current cell to Markdown |
y |
Convert the current cell to Code |
x |
Cut the selected cell |
v |
Paste the cell below |
l |
Toggle line numbers |
Shift + Tab |
View function documentation |
Ctrl + S |
Save and checkpoint |
Shift + Enter |
Run cell and select or create the next cell |
Ctrl + Enter |
Run the current cell |
Alt + Enter |
Run the current cell and insert a new one below |
- Expand/Collapse: Toggle the blue cell marker on the left to hide or show long outputs.
- Clear Outputs: Use the Notebook -> Clear All Outputs menu to reset the view.
- Comments: Use the
#symbol for code comments. They are ignored by the Python Kernel.
Run more statements like below to get more practice:
# Variables and arithmetic
width = 15
height = 10
area = width * height
print(area)
How Notebooks Work
A notebook operates as a web server—a process that runs in an infinite loop and listens for browser connections. When you request a page, the server returns an HTML document.
While the server manages the interface, it does not execute Python code directly. Instead, it connects to a Python Kernel (a separate process). When you run a cell, the server submits the code to the Kernel, receives the processed results, and returns them to the browser for display.
Useful Magic Commands
Jupyter provides "Magic" commands to extend functionality:
- Last Output: The interpreter automatically displays the last computed value. This is also accessible via the built-in variable
_(underscore), which is particularly useful for interactive computing. - Timing Execution: Use
%%timeat the start of a cell to measure the execution time of its contents. - Bash Integration: If you are familiar with the Command Prompt, you can use
%%bashto run terminal commands directly within a notebook cell.
%%time
# Accessing the last output via _
2 ** 100
_
Bash example
%%bash
pwd
After all, computers process logic but don't care about readability, but humans care!
Practice Code
Try adding code cells to practice variables and arithmetic. Remember, you can use the hash symbol (#) for comments; these are ignored by the Python Kernel.
# Comments are not executed
width = 15
height = 10
area = width * height
print(area)
Shutting Down
- Shut Down a Kernel: Use the Running tab (circle icon on the left) to stop specific notebook kernels.
- Stop the Server: Press Ctrl + C twice in the Terminal/Command Prompt window where you started Jupyter.

- While this book provides the essentials, you can refer to the official Jupyter documentation for a comprehensive guide.
Running a Python Script
While notebooks are great for exploration, you often run Python code as scripts (.py files):
- Create the File: Save a text file as
hello.pywith the content:print("Hello, Python!"). - Open Terminal: Navigate to the file's directory using the
cdcommand. - Execute: Run the command
python hello.py.
