import numpy as np
import matplotlib.pyplot as plt
= np.linspace(0, 10, 100)
x = np.sin(x)
y
plt.plot(x, y)'x')
plt.xlabel('sin(x)')
plt.ylabel('Sine Wave')
plt.title( plt.show()
Notebook cells
In a Jupyter Notebook, notebook cells are the fundamental building blocks that allow you to organize, create, and execute various elements within the notebook. They are like blocks that can contain code or text, and you can run them to see the results immediately. This makes Jupyter Notebooks interactive and easy to use.
Types of cells
There are three main types of cells in a Jupyter Notebook: - Code Cells - Markdown Cells - Raw Cells
Code Cells
These cells contain executable code written in the programming language of the kernel you are using (e.g., Python, Julia, R). When you run a code cell, the output is displayed right below the cell.
Markdown Cells
These cells contain text formatted in Markdown, a lightweight markup language that allows you to create formatted text using simple syntax. You can use Markdown to create headings, lists, links, images, and more.
Raw Cells
Raw cells contain text that is not executed or rendered by the notebook. It is used when you want to keep text “as-is,” without any formatting or syntax highlighting. This can be useful for storing comments or instructions in the notebook that are not intended to be rendered or executed.
Creating, Editing, and Deleting Cells
Creating Cells:
- To create a new cell, click the ‘+’ button in the toolbar located at the top of the notebook. A new cell will appear below the currently selected cell.
- Alternatively, you can use the ‘Insert’ menu and choose ‘Insert Cell Above’ or ‘Insert Cell Below’ to add a new cell relative to the current one.
By default, new cells are created as code cells, but you can change the cell type using the dropdown menu in the toolbar or the ‘Cell Type’ option in the ‘Cell’ menu.
Editing Cells:
- To edit a cell, simply click on the cell you want to modify. The cell will enter edit mode, and you can type or modify the cell’s content.
- When you’re done editing, press ‘Enter’ to create a new line within the cell or ‘Esc’ to exit edit mode.
Deleting Cells:
To delete a cell, first click on the cell you want to remove. This will select the cell.
There are several ways to delete the selected cell:
Click the ‘Cut Cells’ button in the toolbar.
Go to the ‘Edit’ menu and click ‘Delete Cells’.
In command mode, use the keyboard shortcuts ‘dd’ or ‘X’.
By creating, editing, and deleting cells, you can flexibly structure your Jupyter Notebook to accommodate different types of content and support your projects’ requirements.
Running and interrupting cells
Running Cells:
Executing or rendering the content of a cell in Jupyter Notebook is called “running” a cell. Depending on the cell type, running the cell will either execute the code (code cell) or render the text with proper formatting (Markdown cell).
To run a cell, you have several options:
Click the ‘Run’ button in the toolbar located at the top of the notebook. This will run the current cell and automatically select the next cell.
Use keyboard shortcuts to run the cell:
‘Shift+Enter’: Runs the current cell and selects the next cell.
‘Ctrl+Enter’: Runs the current cell and remains in the same cell.
‘Alt+Enter’: Runs the current cell and inserts a new cell below.
Interrupting Cells:
Sometimes, you may need to stop a cell from running, especially when running code that takes a long time to execute or when you realize that the code is incorrect or incomplete.
To interrupt the execution of a cell, you can:
Click the ‘Interrupt Kernel’ button (a square button) in the toolbar.
Go to the ‘Kernel’ menu in the top menu bar and click ‘Interrupt’.
Interrupting a cell stops the current execution, allowing you to fix issues, change the code, or simply stop a long-running process before it completes. You can then re-run the cell or continue running other cells as needed.
Cell output and inline display
Cell Output:
When you run a code cell in Jupyter Notebook, the output generated by the code (e.g., printed text, calculated values, or error messages) will appear directly below the cell. This immediate display of output allows you to see the results of your code execution without having to switch between different windows or applications. The output is automatically saved with the notebook, so you can review it later or share it with others.
Inline Display:
In addition to text output, Jupyter Notebook supports inline display of rich media elements such as images, charts, plots, and even interactive widgets. This feature allows you to present visualizations and other multimedia content directly within the notebook, enhancing the readability and interpretability of your analysis. Most popular data visualization libraries, like Matplotlib, Seaborn, or Plotly, have built-in support for rendering charts and plots inline in Jupyter Notebook.
For example, to create a simple plot using Matplotlib and display it inline, you can use the following code in a code cell:
After running the cell, the sine wave plot will be displayed directly below the code cell, making it easy to analyze the result and share the visualization with others.