Recap and Today’s Theme
Hello! In the previous episode, we covered the setup of Python development environments using Anaconda, making it easy to create virtual environments and manage Python and libraries efficiently. Today, we will learn how to use Jupyter Notebook, an interactive development tool included in Anaconda. Jupyter Notebook is widely used in data science and AI development because it allows you to run code, visualize data, and create reports all within a single environment. Let’s dive into the basic operations of Jupyter Notebook!
What Is Jupyter Notebook?
Jupyter Notebook is a development tool that allows you to run Python code interactively in a browser. It enables efficient data analysis, AI model development, and visualization because you can see the results of code execution instantly. Additionally, you can create documentation using Markdown, making it easy to combine code and explanations in reports.
Key Features of Jupyter Notebook
- Interactive Development Environment: Execute code in individual cells and see results immediately.
- Markdown Support: Write documentation alongside code, enabling easy report creation with formatted text.
- Rich Visualization Tools: Easily visualize data with graphs and figures displayed directly within cells.
How to Launch Jupyter Notebook
Jupyter Notebook can be easily launched within an Anaconda environment. Follow these steps to get started:
1. Open Anaconda Prompt (or Terminal)
Open Anaconda Prompt (Windows) or Terminal (macOS/Linux). Jupyter Notebook should ideally be run within a virtual environment, so activate your virtual environment:
conda activate myenv # Replace "myenv" with your environment's name
2. Launch Jupyter Notebook
With the virtual environment activated, run the following command:
jupyter notebook
This command will open your default browser, displaying the Jupyter Notebook home screen. From this screen, you can create a new notebook or open an existing one.
Basic Operations in Jupyter Notebook
1. Creating a New Notebook
- Click the “New” button in the top right corner of the home screen and select “Python 3”. This creates a new notebook.
- Click on the notebook title to rename it, for example, “My First Notebook.”
2. Using Cells
The basic building blocks of Jupyter Notebook are cells. There are two main types of cells:
- Code Cell: For writing and executing Python code.
- Markdown Cell: For writing text and equations. Use these cells to add explanations or comments.
3. Running Code Cells
Write Python code in a code cell and execute it:
print("Hello, Jupyter Notebook!")
Select the cell and press Shift + Enter to run the code. The output will appear below the cell, and the cell’s left side will show the execution order (e.g., In [1]:
).
4. Using Markdown Cells
Markdown cells let you add explanations and documentation. Select a cell, choose “Markdown” from the toolbar dropdown menu, and write:
# This is a header
## This is a subheader
- Bullet point 1
- Bullet point 2
**Bold text** and *italic text* are supported.
You can also write equations: $E = mc^2$
Press Shift + Enter to display the formatted text.
5. Adding, Deleting, and Moving Cells
- Adding a Cell: Click the “+” button in the toolbar to add a new cell.
- Deleting a Cell: Select a cell and click the scissors icon in the toolbar to delete it.
- Moving a Cell: Select a cell and use the up and down arrows in the toolbar to move it.
Data Visualization and Extensions
Jupyter Notebook excels when used with visualization libraries and extensions.
1. Displaying Graphs
Let’s create a graph using the Matplotlib library:
import matplotlib.pyplot as plt
# Prepare data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Plot the graph
plt.plot(x, y)
plt.title("Sample Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Running this code displays the graph directly within the notebook, demonstrating how you can visualize data alongside your code.
2. Utilizing Extensions
Jupyter Notebook offers many useful extensions (Jupyter Notebook Extensions). By installing and activating them, you can add features such as code auto-completion and cell folding. To install these extensions, run the following commands in Anaconda Prompt:
conda install -c conda-forge jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
After installation, a new Nbextensions tab appears in Jupyter Notebook, where you can toggle various extensions on or off.
Summary
This episode covered the basics of Jupyter Notebook, a powerful tool for running Python code interactively and visualizing results instantly. The ability to create Markdown cells for explanations and reports makes Jupyter Notebook indispensable in data science and AI development.
Next Episode Preview
Next time, we will explore NumPy, a numerical computing library. NumPy is a powerful tool for fast numerical operations, and we will learn its basic operations and how to use it effectively in AI development. Stay tuned!
Annotations
- Markdown: A markup language for formatting plain text.
- Matplotlib: A popular library for creating graphs and plots in Python.
- Jupyter Notebook Extensions: Additional features that can be added to enhance the functionality of Jupyter Notebook.
Comments