5 Jupyter Notebook
The Anaconda Distribution includes Jupyter Notebook, a powerful tool for interactive computing.
Jupyter Notebook is an interactive web-based environment where you can write and run Python code, integrate narrative text with Markdown, and display outputs like plots directly within the web interface.
Unlike the Python IDLE, which operates primarily as a basic editor, Jupyter Notebook enhances your interactive experience with these features:
- Write and Execute Code: Each interactive “cell” in a Jupyter Notebook can contain Python code, which you can execute independently.
- Combine Text and Code: Easily switch a cell to “Markdown” mode to integrate narrative text, formatted instructions, or even mathematical equations using LaTeX.
- Visualize Results: Display charts, graphs, and other visualizations directly within the notebook, facilitating instant feedback loops beneficial for exploratory data analysis.
5.1 Launch Jupyter Notebook
- Open Anaconda Navigator:
- Windows: Open the Start Menu and search for “Anaconda Navigator.” Click to launch it.
- macOS/Linux: Search for “Anaconda Navigator” in your applications or open a terminal and type:
anaconda-navigator
- In Anaconda Navigator, locate the Jupyter Notebook application.
- Click the “Launch” button. This opens Jupyter Notebook in your default web browser.
5.2 Create Your First Notebook
- In Jupyter’s web interface, navigate to the
code
directory (refer to our folder structure explanation). - Click “New” in the top-right corner and select “Python [default environment]” to create a new notebook.
- A new notebook page opens with an empty code cell.
5.2.1 Writing and Running Code
Type any Python code in the first cell, such as:
print("Hello, Jupyter!")
Execute the code by pressing
Shift+Enter
or clicking “Run.” The output displays directly below the cell.
5.2.2 Adding Markdown Text
Click the “+” button to add a new cell.
Change the cell type from “Code” to “Markdown” using the dropdown menu.
Write your text in Markdown, for example:
This is **bold** markdown
Render the Markdown as formatted text by pressing
Shift+Enter
.
5.2.3 Save the Notebook
- Use the “File” menu to save the notebook.
- Name the file
hello-jupyter.ipynb
and save it in thecode
folder.
.ipynb
files are documents produced by Jupyter Notebook. These files contain both the inputs (code, Markdown text) and outputs (code execution results, visualizations) of interactive sessions. This format enables you to capture a complete and reproducible record of your computation process, enhancing collaboration and sharing in data science projects.
Each .ipynb
file is structured as a JSON document, containing an ordered list of “cells” which can be code, Markdown, or raw text. This structure allows you to execute code in pieces, revisit earlier results, or make changes without rerunning the entire script, unlike traditional .py
scripts which run sequentially from start to finish.