Introduction to Jupyter

In this chapter, we will provide a quick tour of the Jupyter Notebook programming environment.

The Jupyter Notebook App

Jupyter Notebook is an open source, browser-based, programming environment. It supports a number of of programming languages such as R, Python, and Julia, but is primarily used for writing Python code. Jupyter uses a notebook file format that supports the inclusion of formatted text, embedded media, and executable code all within the same document. The extension for notebook files is .ipynb, which stands for IPython Notebook (the original name of Jupyter Notebook).

Jupyter notebook has two primary components: The dashboard and the kernel. The dashboard is a browser-based application that you will use to create, edit, and run your notebook documents. The kernel is a program that serves as a bridge between the dashboard and the Python interpreter.

The Notebook Format

Every notebook document created in Jupyter Notebook is a collection of cells. There are three types of cells that can be included in a notebook document: code cells, markdown cells, and raw cells.

  • A code cell contains lines of executable Python code. The output of the cell is displayed directly beneath the cell.

  • A markdown cell is used to create a section of formatted text. The formatting in a markdown cell is defined using the markdown language, which is a language designed specifically for formatting text.

  • A raw cell contains unformatted, mono-type, non-executable text.

We will work almost exclusively with code cells and markdown cells in this course. Let’s discuss these cell types more in more detail.

Code Cells

Code cells are used to contains sections of executable Python code. A code cell can contain as many lines of code as you would like. To execute the code contained in a code cell, you would select the cell by clicking inside of it or directly to the left of it, and then typing [CTRL] + [ENTER]. If the code inside the cell produces output, that output will be displayed directly beneath the cell.

To the left of every code cell, you will find a sequence of characters similar to the following In [ ]. If the cell has previously been executed, the brackets will contain a number inside of them. This numbers that appear in the brackets next to code cells will indicate the order in which they were executed.

An example of a code cell is provided below.

x = 42
y = 37
print('Hello, world!')
print('The sum of x and y is:', x + y)
Hello, world!
The sum of x and y is: 79

Markdown Cells

A Markdown cell contains formatted text. Jupyter does not have tools that can be used to format text, as you would find in a word processing program. Instead, it uses Markdown. Markdown is a language developed specifically for formatting text.

When creating a Markdown cell, you indicate the desired formatting for certain sections by using special sequences of characters. For example, if you want to indicate that a piece of text should appear in bold, then you would place two * symbols on each side of the text, as shown below.

**This sentence would appear in bold.**

After creating a markdown cell, you can put it into edit mode by double clicking on it. When you are done editing the cell, you can render it as formatted text by typing [CTRL] + [ENTER].

We will not be providing further examples of Markdown syntax here, but will provide you with two references for learning more about Markdown:

  1. Markdown Cheat Sheet

  2. Markdown: Basic Syntax

Cell Operations

When working with a notebook document, you will frequently need to create new cells, and delete existing cells. We will now describe how to perform several common cell operations.

  • Creating New Cells. You can create a new cell above or below a currently existing cell by clicking on the existing cell and typing [a] for above, and [b] for below. The new cell will be created as a code cell.

  • Deleting Cells. To delete a cell, simple select the cell by clicking to the left of it, and then type [d] twice.

  • Converting Cell Types. You can convert cells from code cells to Markdown cells, and vise-versa. To do so, select the cell by clicking to the left of it. You can then press [M] to convert the cell to a Markdown cell, or press [Y] to convert it to a code cell.

Kernel Actions

The kernel associated with a notebook document represents the Python session running behind the notebook. Any variables created by running a code cell will be stored within that Python session. The Python session will automatically be closed, and the variables within it cleared from memory, when the notebook is closed. However, there are often times when you would like to manually restart the Python session while you are actively working on it. This, and similar tasks, can be performed using commands found with the Kernel menu item in Jupyter Notebooks. We will describe the purpose of a few of these commands.

  • Interrupt will stop the kernel from processing commands. This can be useful if you have executed a code cell whose contents are taking longer to run that you had expected. The interrupt command will not reset your Python session, and will not clear any variables.

  • Restart will reset your Python session, clearing from memory any variables that were defined within that session. The Restart command will not clear any output from the notebook. Any cells that were previously executed will still show their results, but any variables created to generate those results will no longer exist in memory.

  • Restart & Clear Output will reset the Python session, and will clear the output from any cells that have been previously executed. In general, any time you open a notebook document that you have worked on in the past, I recommend that you start by running this command. That way there will be no confusion about whether you have executed a certain cell in this session, or in a previous session.

  • Restart & Run All will reset the Python session, clear the output from any cells, and then will run every code cell in the notebook from top-to-bottom. To make sure that your code is functioning properly, I recommend that you run this command periodically. You will be required to run this command as your last step before submitting any assignment.

Non-Linear Execution in Jupyter

We are not required to execute the cells in a notebook document in order, from top to bottom. We can execute the cells in any order that we wish, and we can execute individual cells as many times as we like.

This can cause some problems, however. It is possible that the output of our cells will depend on the order in which we execute them. If you do not execute them in order, from top-to-bottom, then it might be unclear to someone reading your notebook (for instance a professor) how you are getting the displayed results. The reader won’t have any way of knowing what order that you executed the cells in. For that reason, you will be required to run the command Restart & Run All before submitting any assignment in this course.

Magic Commands

A magic command is a command that can be given directly to the Python kernel from within a code cell. Magic commands are not written in Python code, but have their own syntax. Every magic command begins with one or two percent symbols.

Magic commands can be used to perform a variety of tasks, such as calculating the execution time of a code cell, running an external script, or specify formatting options. We will not be working with magic commands frequently in this course. However, if you are interested in learning more about magic commands, you can find a list of available commands here: Magic Commands

As one example, in the cell below, we use the magic command %%time to calculate the execution time for a code cell. Don’t worry about that the code in the cell is actually doing at this moment. We will cover that later in the course.

%%time

i = 0
while i < 10**8:
    i = i + 1
    
Wall time: 8.38 s