Your First Notebook in VS Code

Make a notebook in VS Code, choose the project's .venv Python as its kernel, run code cells with Shift+Enter, see that a cell shows its last line's value while print() shows every line it prints, add a Markdown cell, and save the code, results and notes in one .ipynb file.

Novice · Python · Updated

By

After this lesson you can

  • Make a notebook in VS Code, choose the .venv's Python as its kernel, run code cells with Shift+Enter, and add a Markdown cell
  • Tell what a code cell shows, the value of its last line, compared with every line it prints with print()

Key takeaways

  • A notebook's kernel is the Python that runs its cells. Choose the project's .venv with Select Kernel, so the notebook has the packages you installed there.
  • Shift+Enter runs a cell and moves to the next one, and the cell's result appears under it.
  • A code cell shows the value of its last line. print() shows every line it prints, so use print() when you want to see more than one thing.
  • A Markdown cell holds notes, and
  • Saving the notebook saves the code, the results and the notes together in the .ipynb file.

Sooner or later someone hands you a notebook: a class's homework, a coworker's analysis, a tutorial's example. To use it, or to start your own, you need four moves: make or open the notebook, connect it to the right Python, run its cells, and save. This lesson does all four in VS Code, with a small example you can type in a few minutes.

You need a project folder open in VS Code with a virtual environment, .venv, that has pandas and ipykernel installed, and VS Code's Jupyter extension: the setup lesson before this one shows each step. The example is Bean Street Coffee, a coffee chain with three stores: its owner, Rosa, has a morning's first 12 orders and wants to know how many there were and what they came to.

Make a notebook and choose its kernel

Press Ctrl+Shift+P (Cmd+Shift+P on a Mac), type new jupyter, and choose Create: New Jupyter Notebook. Save it right away with Ctrl+S (Cmd+S), as morning.ipynb in the project folder.

A notebook's cells are run by a kernel: a Python program that starts when the notebook does and stays running in the background, remembering everything the cells make. The kernel decides which packages your code can use, so it must be the project's own Python. Click Select Kernel at the top right, choose Python Environments, then the environment named .venv. Its name then shows at the top right.

VS Code's Select Kernel list for a new notebook, with the project's .venv Python environment at the top, marked as recommended

If VS Code says that running cells requires the ipykernel package, and offers an Install button, the environment you chose doesn't have ipykernel. Click Install, or run pip install -r requirements.txt with the environment on.

Run a code cell

Type Rosa's orders into the first cell. Each order is a dictionary: a drink, a size and a price, each read by its name in quotes, such as order["price"].

orders = [
    {"drink": "latte", "size": "M", "price": 4.45},
    {"drink": "drip coffee", "size": "L", "price": 3.45},
    {"drink": "cold brew", "size": "S", "price": 3.65},
    {"drink": "latte", "size": "S", "price": 3.95},
    {"drink": "mocha", "size": "M", "price": 4.75},
    {"drink": "drip coffee", "size": "M", "price": 2.95},
    {"drink": "chai", "size": "M", "price": 4.35},
    {"drink": "latte", "size": "L", "price": 4.95},
    {"drink": "tea", "size": "S", "price": 2.15},
    {"drink": "drip coffee", "size": "S", "price": 2.45},
    {"drink": "cold brew", "size": "L", "price": 4.65},
    {"drink": "latte", "size": "M", "price": 4.45},
]

Press Shift+Enter. The cell runs, a number in square brackets appears beside it, [1], and VS Code moves to a new cell below. The cell shows nothing, because it only makes a list; the kernel now remembers orders. (The Kernel: Why Run Order Matters explains those numbers.) In the next cell:

len(orders)
12

Ctrl+Enter runs a cell and stays on it, which is handy when you're changing one cell and running it again.

What a cell shows

A cell shows the value of its last line, when that line is a value, as len(orders) was. Add up the prices in the next cell:

total = 0
for order in orders:
    total = total + order["price"]
total
46.2

The last line, total, is the value shown. Now try a cell with two values in it:

len(orders)
total
46.2

Only 46.2: the count on the first line was worked out and then dropped, because only the last line is shown. To see several things, print them. print() shows every line it prints, wherever it is in the cell:

print("orders:", len(orders))
print("total:", total)
orders: 12
total: 46.2

Notes in a Markdown cell, then save

Hover over the top edge of the first cell and click + Markdown to add a Markdown cell above it. Type:

# Bean Street's morning

The first 12 orders of the morning, and what they came to.

# at the start of a line makes a heading. Press Shift+Enter and the cell shows as formatted text; double-click it to edit it again.

Save with Ctrl+S (Cmd+S). The .ipynb file now holds the heading, the code and every result shown under it. Close it and open it tomorrow, and the results are still there.

Bean Street's morning notebook in VS Code: a Markdown title cell, then code cells numbered 1 to 5 with their results, 12, 46.2, 46.2 and the two printed lines, and the .venv kernel named at the top right

Check yourself

2 questions to try on the lesson page.

So what

Choosing the kernel, running cells, knowing what a cell shows and saving are the four moves behind every notebook you'll open, in this course and anywhere you meet one.

Key terms