Getting Started with Jupyter Notebooks
The interactive shell is great for very small experiments. For anything more structured—data analysis, step-by-step tutorials, or experiments you want to save—Jupyter Notebooks are ideal.
Installing Jupyter and JupyterLab
To use Jupyter Notebooks, you'll first need to install Jupyter. If you're using Python, you can install them via pip. Run the following command in your terminal or command prompt:
pip install jupyter
This will install Jupyter Notebook .
Alternatively, you can also install jupyterlab
pip install jupyterlab
Launching Jupyter and Creating a Notebook
From a terminal in your working directory, run:
jupyter notebook
This will open a web interface in your default browser (usually at http://localhost:8888). In the browser:
- Navigate to the folder where you want your notebook.
- Click New → Python 3 (ipykernel) (or similar).
- A new notebook opens with an empty code cell.
Save your notebook with a descriptive name, e.g., intro_shell_and_notebooks.ipynb.
Working with Code Cells in Jupyter
A notebook consists of cells. The most common type is the code cell, where you write and run Python code.
Writing and Running Code Cells
Click in the first cell and write:
message = "Hello from Jupyter!"
message
Run the cell with Shift + Enter (or the Run button in the toolbar).
# Output
'Hello from Jupyter!'
Explanation: Jupyter displays the last expression’s result in a cell. Assignments (message = ...) don’t produce visible output unless explicitly printed or returned as the last expression.
Example: Simple Data Analysis in a Notebook Cell
prices = [19.99, 5.49, 3.5, 12.0]
# Compute basic stats
count = len(prices)
minimum = min(prices)
maximum = max(prices)
average = sum(prices) / count
count, minimum, maximum, round(average, 2)
# Output
(4, 3.5, 19.99, 10.25)
Explanation:
- The list
pricesis analyzed using built-in functions. - The tuple
(4, 3.5, 19.99, 10.25)contains count, min, max, and rounded average. - In notebooks, returning a tuple is a convenient way to show several values at once.
Re-running and Editing Cells
One of Jupyter’s strengths is iterative development:
- Modify the code in an existing cell.
- Re-run it (Shift + Enter).
- Any variables defined or changed apply to later cells.
For example, if you add a new price:
prices.append(7.25)
len(prices), max(prices)
# Output
(5, 19.99)
Explanation: The notebook remembers prices from previous cells. You can build logic step-by-step across multiple cells.
Using Markdown Cells for Documentation
To make notebooks readable and shareable, combine code with Markdown cells for text, explanations, and results.
Creating and Editing Markdown Cells
- Insert a new cell (using the + button or
B/Ain command mode). - Change its type to Markdown (drop-down menu or press
Min command mode). - Write Markdown text, then run the cell (Shift + Enter) to render it.
Example: Markdown for Section Headings and Explanations
In a Markdown cell, you might write:
# Sales Analysis
In this section, we analyze basic statistics of product prices.
- **Count**: number of items
- **Min/Max**: price range
- **Average**: typical price level
Rendered, this becomes a nicely formatted heading with a bullet list.
Combining Code and Markdown for a Reproducible Workflow
- A Markdown cell describes what the next code does.
- A code cell implements the logic.
- Another Markdown cell summarizes or interprets the result.
Example: Documented Computation
Markdown cell:
### Discounted Prices
Apply a 10% discount to each price and compute the new average.
Code cell:
discounted = [round(p * 0.9, 2) for p in prices]
original_avg = round(sum(prices) / len(prices), 2)
new_avg = round(sum(discounted) / len(discounted), 2)
original_avg, new_avg
# Output
(10.25, 9.23)
Explanation: The list comprehension applies a 10% discount. The original and new averages are shown so that the Markdown commentary can interpret the impact.
Summary / Key Takeaways
In this lesson Using the Interactive Shell and Jupyter Notebooks, you have learned:
- Creating Jupyter Notebooks: How to start Jupyter, create new notebooks, and save them with meaningful names.
- Working with Code Cells: How to write, edit, run, and re-run code cells, building state and logic step-by-step.
- Notebook-Based Exploration: How to use notebooks for organized experiments, from simple calculations to multi-step analyses.
- Markdown Documentation: How to add Markdown cells for headings, descriptions, and narrative around your code and results.
- Combining Code and Text: How to structure notebooks as reproducible, readable documents by interleaving code and explanatory text.
- Choosing the Right Tool: How to decide when to use the interactive shell versus Jupyter Notebooks based on your workflow needs.
