Jupyter Notebook

How can we organize our code and see the results?

KEY TERMS

  • Jupyter Notebook: an application that allows us to run and visualize multiple lines of Python code, display charts and graphs, and save and organize our code

  • notebook: a term used to refer to a Jupyter Notebook or .ipynb file

  • code cell: a cell in a Jupyter Notebook that can contain code

Note: This guide on how to use Jupyter Notebook assumes that it has already been installed on your machine. If this is not the case, you can follow the instructions on the Jupyter Notebook website on how to install Jupyter Notebook.

Up until now, we have been using the command python in the Command Prompt in order to write our Python commands. However, you may have noticed that writing code line by line can be time-consuming. The Command Prompt is also not very useful for looking at tables or displaying graphs, which is something that data scientists often need to do.

In this chapter, we will introduce you to Jupyter Notebook. Jupyter Notebook is an application that allows us to run and visualize multiple lines of Python code as well as display various types of charts and graphs.

To run Jupyter Notebook, we can navigate to the directory that our Jupyter Notebook file is in (or the directory in which we want to create a new Jupyter Notebook file) and run the command jupyter notebook in the Command Prompt. This should open a tab in our web browser that will look something like this:

You'll notice that this window looks really similar to our File Explorer! Our current directory is shown in the gray bar and the folders and files in this directory are listed underneath. To open a Jupyter Notebook file (which we will refer to from now on as a notebook), we can double-click on it. This is what a notebook might look like:

To run the code in a code cell (the shaded boxes containing Python code), first click on that cell to activate it. When selected, the code cell will be highlighted with a little green or blue rectangle. Next, either click on the ▶| Run button at the top of the notebook, or hold down the shift key and press the return or enter key.

In Figure 2 above, all of the code cells have been run and the results are displayed below each cell. Note the similarities between Python commands run in the Command Prompt and those run in the Jupyter Notebook. For example, a variable declared in a previous line/cell can still be accessed in a later line/cell.

Try it out! The best way to get familiar with how to use a Jupyter Notebook is to write some code!

Last updated