> ## Documentation Index
> Fetch the complete documentation index at: https://fastapi2day.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Jupyter Notebooks in VS Code: Working with .ipynb Files

> Explore Jupyter notebooks in VS Code — create .ipynb files, run cells interactively, and choose the right notebook environment for your workflow.

Jupyter notebooks are one of the most important tools in the Python data and AI ecosystem. Instead of writing a script and running the whole thing at once, notebooks let you break your code into individual cells and execute them one by one. You see the output — including charts, tables, and formatted text — inline, right below each cell. This makes notebooks ideal for exploring data, building models step by step, and documenting your thinking alongside your code.

## What is a .ipynb file?

A `.ipynb` file (IPython Notebook) is a JSON document that stores a sequence of **cells**. Each cell can contain either Python code or Markdown text. When you run a code cell, the output is captured and saved directly in the file alongside the code that produced it.

This structure makes notebooks great for:

* **Learning** — see what each line does immediately
* **Experimentation** — try an idea, see the result, adjust and try again
* **Documentation** — mix explanations with runnable code
* **Data analysis** — display DataFrames, charts, and statistics inline

## Notebook environments compared

There are several places you can run Jupyter notebooks. Here is a quick comparison:

| Environment           | Best for                                                                     |
| --------------------- | ---------------------------------------------------------------------------- |
| **VS Code Notebooks** | Professional Python development alongside your project code                  |
| **JupyterLab**        | Dedicated notebook-focused workflow with a file explorer and multiple panels |
| **Jupyter Notebook**  | Learning, simple experiments, and quick prototypes                           |
| **Google Colab**      | Cloud-based AI/ML work with free GPU access                                  |

For this course, you will primarily use **VS Code Notebooks**, since they keep your notebooks and Python files in the same workspace.

## Creating a notebook in VS Code

To use notebooks in VS Code you need the **Python** and **Jupyter** extensions installed (covered in the Setup page), plus the `ipykernel` package in your active environment.

<Steps>
  <Step title="Install notebook support">
    Open the VS Code terminal and run:

    ```bash theme={null}
    pip install notebook ipykernel
    ```
  </Step>

  <Step title="Create a new notebook">
    Use either of these methods:

    **Option A — File menu:**

    ```text theme={null}
    File → New File → name it notebook.ipynb
    ```

    **Option B — Command Palette:**
    Press `Ctrl/Cmd + Shift + P`, then search for:

    ```text theme={null}
    Jupyter: Create New Jupyter Notebook
    ```
  </Step>

  <Step title="Select a kernel">
    Click **Select Kernel** in the top-right corner of the notebook and choose the Python interpreter from your virtual environment.
  </Step>

  <Step title="Run your first cell">
    Click in the first cell, type a Python expression, and press `Shift + Enter` to run it.

    ```python theme={null}
    print("Hello from a notebook!")
    ```
  </Step>
</Steps>

## Running code in notebooks

Notebooks support two types of cells:

**Code cells** — contain Python (or shell commands with `!`):

```python theme={null}
# Regular Python
numbers = [1, 2, 3, 4, 5]
print(sum(numbers))
```

**Shell commands** — prefix with `!` to run operating system commands:

```python theme={null}
!python --version
!pip list
```

**Package installation** — use `%pip` (not `pip`) inside notebooks:

```python theme={null}
%pip install pandas
```

<Note>
  Using `%pip install` inside a notebook is preferred over `!pip install` because `%pip` ensures the package is installed into the same Python environment the notebook kernel is using.
</Note>

## Essential keyboard shortcuts

Notebooks have two modes: **Edit mode** (you are typing inside a cell) and **Command mode** (you are navigating between cells). Press `Esc` to enter Command mode and `Enter` to return to Edit mode.

| Shortcut             | Action                               |
| -------------------- | ------------------------------------ |
| `Shift + Enter`      | Run cell and move to the next        |
| `Ctrl + Enter`       | Run cell and stay in current cell    |
| `Alt + Enter`        | Run cell and insert a new cell below |
| `Esc`                | Switch to Command mode               |
| `Enter`              | Switch to Edit mode                  |
| `A` (Command mode)   | Insert cell above                    |
| `B` (Command mode)   | Insert cell below                    |
| `D D` (Command mode) | Delete current cell                  |
| `M` (Command mode)   | Convert cell to Markdown             |
| `Y` (Command mode)   | Convert cell to Code                 |
| `Ctrl/Cmd + S`       | Save notebook                        |

## Google Colab

If you need to run a notebook without a local setup — or want free GPU access for AI experiments — Google Colab is a solid option. It is a cloud-hosted Jupyter environment that requires only a Google account.

Visit [colab.research.google.com](https://colab.research.google.com) or go directly to [colab.new](https://colab.new) to open a blank notebook instantly.

Install packages for the current Colab session:

```python theme={null}
%pip install pandas numpy openai
```

<Tip>
  All practice notebooks in this course are available as Colab links, so you can run them in your browser if you prefer not to set up a local environment right away.
</Tip>

## JupyterLab (standalone alternative)

If you prefer working in a browser-based interface dedicated entirely to notebooks, JupyterLab is the modern upgrade to the classic Jupyter Notebook app.

```bash theme={null}
pip install jupyterlab
jupyter lab
```

JupyterLab includes a file explorer, the ability to open multiple notebooks side by side, a built-in terminal, and a text editor — all in one browser tab. It is a strong choice for data science workflows that revolve around notebooks.

<Warning>
  When working locally with JupyterLab or Jupyter Notebook, always launch the server from inside your project's virtual environment so the kernel has access to your installed packages.
</Warning>
