Dr Seán Carroll

by Dr Seán Carroll

Lesson

Coding Environment Setup

10. Getting to know Jupyter Notebooks

Now we’re ready to dig into the details of actually working with Python in a Jupyter Notebook. We can close our terminal window now and focus on the empty Jupyter Notebook in the code editing panel. Let’s also collapse down the file explorer to give us more space.

Jupyter Notebook interface with file explorer and terminal closed | academy.digilab.co.uk

Figure 1. Jupyter Notebook interface with file explorer and terminal closed.

The content of a Jupyter Notebook is arranged into cells. There are two main types of cell that we’ll use; Markdown cells and code cells. Markdown cells are used for writing text - typically explanatory text that accompanies the code we’re developing in a Notebook. The text is formatted using a common markup language called Markdown. The code cells contain executable Python code.

Notebooks follow all of the usual conventions you’re familiar with, e.g. we can save our progress by selecting Save or Save As from the File menu or typing Command+S. In addition to watching this lesson, I encourage you to have a poke around to get to know the environment and what options and functions are available.

Code cells


The default cell type is a code cell, we can type any Python code into a code cell and then run the cell by clicking the play button beside the cell or typing Shift + Enter. If the code generates any output, this is printed below the cell. You’ll notice a green tick and a duration in seconds at the bottom of the cell, indicating that the code was executed without errors and the time that execution took. As an example, the result of executing 2+2 is shown below.

Jupyter Notebook indicating code cell, cell type selection dropdown and code cell menu | academy.digilab.co.uk

Figure 1. Jupyter Notebook indicating code cell, cell type selection dropdown and code cell menu.

When a cell is active or in focus, we have a menu in the top right corner of the cell. There are a number of options in this menu:

  • we can execute the code in the cell line by line - this is very helpful when debugging and trying to find errors.
  • we can execute the code in the cells above the current one.
  • we can execute all of the code in the current cell and all of the following cells.
  • we can split the cell in two at the current cursor location.
  • we can expand a menu of additional options
  • we can delete the cell. This is by far the most commonly used option in this menu.

The bottom right corner of the cell indicates the cell type, a Python code cell, in the case of the cell in Fig. 2. If we want to change the cell type, we can click on the word Python in the bottom right corner to reveal a list of options. Markdown and Python are the only options we’ll be making use of in this course.

If your Notebook ever crashes or you write some bad code, for example, a loop that runs indefinitely, you can simply restart your kernel by hitting the Restart button above the first Notebook cell. This will restart your Notebook kernel and escape the bad code execution.

Markdown Cells


The second cell type is the markdown cell. These are where we write text to accompany our code. Markdown cells are one of the huge advantages we get when using Jupyter Notebooks, over basic Python (.py) script files. They allow us to embed well-formatted text alongside our code. This vastly improves the user experience since we can document our development work as the code evolves.

You’ll see me and the other course instructors making use of Markdown syntax to format our text and Latex to typeset math. Latex and Markdown are beyond what we’ll be covering in this course, but basically, Latex is a typesetting language that makes it extremely easy to typeset equations.

I don’t suggest you spend any time studying Latex or Markdown syntax. Instead, just start trying to use them as you need them. If you need to know how to create bold text in markdown, for example - just google it! Honestly, it’s much more efficient to just pick this stuff up as you need it; we call this just in time learning.

Quality-of-life improvements and shortcuts


You will want to get familiar with some basic keyboard shortcuts to speed things up for yourself. We can change a Python code cell into a Markdown cell, by selecting Markdown from the menu revealed at the bottom right corner of the cell, or by typing ESC + m while the cell is selected or focused.

We can quickly switch back to a code cell by typing ESC + y. Again, I can’t emphasise enough how much faster working in a notebook is once you nail down a handful of keyboard shortcuts.

Next, I’ll summarise some simple shortcuts and small quality-of-life improvements that will make things that much quicker and easier while you’re working in VS Code.

Split screen

Often when we’re working simultaneously across multiple Notebooks, it can be helpful to have both on the screen at the same time. We can achieve this by hitting the split screen button in the top right corner of the window.

If we have multiple Notebooks open, we can achieve the same thing by dragging one of the Notebook tabs over to the right of the screen. Split screen also works well if you have one long Notebook open and want to focus on two different parts of the Notebook at the same time.

Collapsing and re-ordering code cells

In long Notebooks, it can be helpful to collapse code cells. This can make it easier to focus on a specific part of the notebook and also make it easier and quicker to navigate. We do this by clicking on the vertical blue bar to the left of an active cell.

We can also reorder cells by clicking (the blue bar) and dragging the cell up or down to change its position. Keep in mind, however, that the code in your Notebook is executed sequentially. So reordering your code cells can cause errors if, after reordering, you refer to a variable before defining it.

Text editing tricks

  • When editing text, you can move a single line or highlighted block of text up or down by holding the ALT key and hitting the up or down arrow. This is particularly handy for reorganising text.
  • By holding the ALT+SHIFT keys and hitting the up or down arrow you can duplicate the current line of text.
  • Often we have many lines of repeated text that we want to make the same edits to. We can speed this process up by adding multiple cursors to the page at the same time and typing the edits once. This way, the edits are duplicated at each cursor location. To add multiple cursors to the screen, simply hold the ALT key and click the required location of the additional cursors.
  • Finally, we can select multiple instances of the same selection by highlighting one instance of the word we want to select and typing CMD + d. VS Code will then automatically highlight and select the next instance of your selection. If you type CMD + d again, VS Code will highlight the next instance.

Each one of these shortcuts, individually, only saves seconds or fractions of a second. But, when stacked on top of each other, the time savings, over the course of a day, start to become appreciable!

That wraps up what I wanted to cover in this section. We’ve now covered all of the groundwork required to get coding. There’s still a lot more to VS Code, but what we’ve covered so far is more than enough to allow us to move forward.

Don’t worry if you don’t remember everything we covered in this section. This will all start to become second nature once we start working on some Python coding skills in the next section.