Chapter 2: Hello notebook

A Jupyter notebook is a browser application where you can write, run, remix and republish code.

It is free software you can install and run like any other open-source library. It is used by scientists, scholars, investors and corporations to create and share their research.

It is also used by journalists to develop stories and show their work. Examples include:

Install JupyterLab

Now we will return to Pipenv and use it to install JupyterLab, the web-based interactive development environment for Jupyter notebooks, code and data.

pipenv install jupyterlab

Create your first notebook

Now we can use pipenv’s run command to start JupyterLab from your terminal.

pipenv run jupyter lab

That will open up a new tab in your default web browser that looks something like this:

../_images/labpreview.png

Click the “Python 3” button in the middle panel and create a new Python 3 notebook.

Write Python in the notebook

Now you are all setup and ready to start writing Python code.

Do not stress. There is nothing too fancy about it. You can start by just doing a little simple math.

Type the following into the first box, then hit the play button in the toolbar above the notebook (or hit SHIFT+ENTER on your keyboard).

2+2
../_images/2_plus_2.png

There. You have just written your first Python code. You have entered two integers and added them together using the plus sign operator.

Not so bad, right?

Note

If you get an error after you run a cell, look carefully at your code and see that it exactly matches what’s been written in the example. Don’t worry.

Code crashes are a normal part of life for computer programmers. They’re usually caused by small typos that can be quickly corrected.

This to-and-fro of writing Python code in a notebook cell and then running it with the play button is the rhythm of working in a notebook. Over time you will gradually stack cells to organize an analysis that runs from top to bottom.

The cells can contain variables, functions and other Python tools.

A simple example would be storing your number in a variable in one cell …

number = 2

… then adding it to another number in the next.

number + 3

Run those two cells in succession and the notebook should output the number five. Change the number value to 3 and run both cells again and it should output six.

Note

If you’ve never written Python before, we recommend An Informal Introduction to Python and subsequent sections of python.org’s tutorial.

Once you’ve got the hang of making the notebook run, you’re ready to introduce pandas, the powerful Python analysis library that can do a whole lot more than add a few numbers together.