Jupyter Notebook
What are Jupyter Notebook and JupyterLab?¶
Jupyter (Julia, Python, R) Notebook is the original interface of Project Jupyter and is an interactive environment that allows you to write and execute code, visualize data, and document your work all in one place. It supports various programming languages like Python, R, and Julia, but it's most commonly used with Python. It is widely used for data science, machine learning, and research.
JupyterLab, on the other hand, is the next-generation interface of Jupyter. It builds upon the functionality of Jupyter Notebooks and provides a more flexible and powerful environment for working with code and data. JupyterLab is the main interface where Jupyter notebooks are created, edited, and managed alongside other files (such as scripts, terminals, CSV files, or Markdown documents) in customizable panels and tabs.
Here we show the basics of working with a Jupyter notebook to write text and code and save it in different formats. For detailed information on Jupyter Notebooks, refer to JupyterLab's user guide.
Working with Jupyter Notebooks¶
You can write either code or text, including equations, in each cell of a notebook and execute code interactively and visualize and analyze data, variables and plot the data.
Open a Notebook¶
First you open a Jupyter Notebook, which is automatically saved in your environment. The default path is your home directory. But if you change into a directory and then open a notebook, it is saved in the current directory. You can rename your notebook by right-clicking and choosing Rename.
Write Text¶
To convert a cell into a text cell, select “Markdown” from the dropdown menu at the top of the notebook (where Code is displayed by default). Alternatively, you can press the m key.
Pressing Shift + Enter executes the cell and displays the formatted text. Alternatively, you can use the ▶ button in the toolbar.
Add Code¶
You can write program code in a code cell. Executing the cell works the same way as with a text cell.
Edit Notebooks¶
Cells can be moved using drag-and-drop or by clicking the icons on the right side of the cell. Cells can also be deleted or added there.
Export Notebooks¶
When you are finished with your notebook, you can export it to other formats including Markdown, PDF, etc. To do this, go to the File/Save and Export Notebook As ... menu.
Notes on Kernels¶
Jupyter Notebooks are powered by so-called kernels, which execute the written code. Since each kernel consumes memory and computing resources, kernels that are no longer needed should be terminated. For more information, visit the pages Managing Kernels and Terminals and JupyterLab's User Guide for more information.
The following example (in Python) can be used for practice. It demonstrates how a dataset can be loaded, analyzed, and visualized in a Jupyter Notebook.
Example: Analyzing and Visualizing Data in Python using a Jupyter Notebook¶
Step 1: Import Libraries¶
First, the necessary libraries for data analysis and visualization are loaded.
import pandas as pd
import matplotlib.pyplot as plt
We use:
-
pandas to work with data.
-
matplotlib to create visualizations.
Step 2: Load a Dataset¶
Next, let's load a sample dataset using pandas. In this case, we'll use the well-known Iris dataset, which contains information about different types of flowers.
# Load Iris dataset
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
data = pd.read_csv(url)
# Display the first five rows of the dataset
data.head()
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
The code above fetches the dataset from a public URL and displays the first five rows of data.
Step 3: Data Analysis¶
In this step, the average petal length is calculated for each iris species as an example.
# Calculate average petal length by species
average_petal_length = data.groupby('species')['petal_length'].mean()
average_petal_length
species
setosa 1.462
versicolor 4.260
virginica 5.552
Name: petal_length, dtype: float64
Step 4: Data Visualization¶
Finally, a simple bar chart is created to visualize the average petal length for each species.
# Create a bar plot for average petal length by species
average_petal_length.plot(kind='bar', color=['skyblue', 'lightgreen', 'salmon'])
plt.title('Average Petal Length by Species')
plt.ylabel('Petal Length (cm)')
plt.xlabel('Species')
plt.show()
Useful Links:




