Section III: Basic Notebook Operations
Once your environment is set up and you’ve launched your computational notebook (such as Jupyter or Google Colab), there are several basic operations that you need to be familiar with to navigate, create, and manage your notebook effectively. This section will guide you through the essential notebook operations.
1. Notebook Interface Overview
When you first open a computational notebook, the interface is divided into several parts:
- Menu Bar: At the top of the notebook, you’ll find options for performing various tasks, such as saving the notebook, running cells, inserting new cells, and exporting the notebook.
- Toolbar: Below the menu bar, the toolbar contains shortcuts for common actions like adding cells, running cells, moving cells up or down, and interrupting kernel execution.
- Cells: The main body of the notebook consists of individual cells. Cells are where you write your code, display outputs, and add text explanations. There are two main types of cells:
- Code Cells: Where you write and execute code (e.g., Python code).
- Markdown Cells: Where you can add text, headings, and rich media like images or equations using Markdown.
2. Creating and Managing Cells
Creating New Cells: - To add a new cell, click on the + button on the toolbar or use the Insert menu and select Insert Cell Below/Above. New cells can be inserted below or above the current cell.
Running Cells: - To execute a code cell, click on the cell and press Shift + Enter, or click the Run button on the toolbar. The output will appear directly beneath the cell. - You can also run all cells in the notebook using the Run All option from the Cell menu.
Deleting Cells: - To delete a cell, select the cell and click on the scissors icon on the toolbar, or use the keyboard shortcut DD (press D twice).
Moving Cells: - You can move cells up or down using the up/down arrows on the toolbar or by clicking and dragging the cells.
Changing Cell Type: - By default, new cells are code cells, but you can change them to markdown cells. To do this, select the cell and choose Cell Type → Markdown from the Cell menu or use the dropdown on the toolbar.
3. Writing and Running Code
Computational notebooks allow you to write code in code cells and execute them interactively. Here’s how to work with code:
Write Code in Code Cells: - Simply click inside a code cell and start typing your Python (or another supported language) code. For example, you can write:
print("Hello, World!")
## Hello, World!
Execute Code: - After writing the code, press Shift + Enter to run the cell. The output will be displayed immediately below the cell.
Kernel and State Management: - The kernel is the computational engine that runs your code in the notebook. You can manage the kernel through the Kernel menu: - Restart Kernel: This will clear the current state of the notebook and variables in memory but keep the code. - Interrupt Kernel: Use this if your code is stuck or taking too long to execute.
4. Markdown for Text and Formatting
Markdown cells allow you to format text, headings, lists, and even add images or equations. Here are some common Markdown operations:
Text Formatting: - Write plain text in the cell and execute it to display formatted text. You can apply: - Bold: **bold** or __bold__ - Italics: *italics* or _italics_ - Headings: Use #, ##, ### for heading levels - Lists: - Bulleted list: * Item 1, * Item 2 - Numbered list: 1. Item 1, 2. Item 2
Inserting Images: - To insert an image in a markdown cell, use the following syntax:
![Alt Text](URL_of_the_image)
Mathematical Equations: - You can add mathematical equations using LaTeX syntax. For example, to write the quadratic formula:
$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$
5. Viewing and Saving Outputs
Viewing Output: - Code cells can produce various outputs such as text, tables, plots, and even interactive widgets. Once a cell is executed, the output is displayed directly beneath the code cell. For instance: - Text Output: Printed statements or results. - Tables: Dataframes or results from libraries like Pandas. - Visualizations: Graphs and plots generated by libraries like Matplotlib or Seaborn.
Saving Your Notebook: - It’s important to save your work regularly. In Jupyter Notebook, you can save the current state of the notebook by: - Clicking the Save icon (floppy disk) on the toolbar. - Selecting File → Save and Checkpoint from the menu. - Using the keyboard shortcut Ctrl + S (or Cmd + S on macOS).
- Autosave: Jupyter Notebooks automatically save your work at regular intervals, but manually saving ensures your progress is stored.
- Exporting Notebooks: You can also export your notebook into different formats for sharing or publishing. To export, go to File → Download as and choose a format:
- HTML: Useful for sharing read-only versions.
- PDF: For printing or formal documentation.
- .py: Python script for sharing the code outside the notebook environment.