Introduction to Jupyter Notebooks for Python

Pedro Henrique
6 min readFeb 5, 2022

--

Photo of a notebook on a leather background
Photo by Luis Soto on Unsplash

A Jupyter Notebook is a web application that allows you to write and share documents that have interactive code and text. You can run it locally on your machine or remotely on a server, it is also used trough some services as Databricks, Google Colab, and others.

It uses a simple markdown language for text formatting and works with a lot of other languages beyond Python. Jupyter is very good if you want to execute code in an interactive way, that is why it is used for exploratory data analysis, numerical simulations, statistical modeling e even machine learning.

Because a Jupyter Notebook makes it easy to run code and take notes, it is a great tool for learning Python. You can save the code and the explanation on the same place!

Let’s see how you can use Jupyter Notebook with Python on your local machine.

Installing Jupyter notebook

It is possible to install it in two ways, trough pip or Anaconda.

Pip is the oficial package manager for Python, trough it you can install several other packages and dependencies that you will need on your projects. If you already have Python (Python 3, please) installed and ready on your machine, just use the command pip install notebook and everything will be working.

Anaconda is a kit of data science tools that already delivers Python with all other tools commonly used on the field. If you are new to Python and doesn’t have a functional environment yet, this way you will have easy access to a lot of things you will eventually use. Just download and install it from Anaconda’s website.

Running the Notebook

After the installation is complete, you can easily run the Jupyter notebooks by navigating to where you want your notebooks to be saved e.g. cd /path_to_my_folder and running jupyter notebook on your terminal. The reason we navigate to the folder first is that Jupyter will use your current folder to store the files it creates. You should see an output similar to this:

Output from Jupyter Notebook on terminal
Output from Jupyter Notebook

Jupyter Notebook’s server will be initiated on your default web browser and you will see something similar to the image below. If your browser doesn’t open automatically, try navigating to http://localhost:8889/tree

Jupyter notebook openned on a browser
Jupyter Notebook on the browser

That is the initial page for Jupyter Notebook. It has three panes: Files, Running and Clusters. The default pane is Files, here you will visualize, create and open your notebooks.

Creating a Jupyter Notebook

It is very easy to create a notebook. Just click on New at the right side and then select Python 3 from the menu. The notebook will be created and open at a new tab, you will see something similar to:

New notebook open on a browser window
The new notebook

Your notebook is created, but doesn’t have a title yet. Click on Untitled, at the top of the page and add the notebook’s title. When you change the title, your notebook will be saved to a new file with the same name as the notebook and the extension .ipynb. At the initial screen of Jupyter Notebook you will see your new notebook with the name you gave and the status running.

When you change the title, your notebook will be saved to a new file with the same name as the notebook and the extension .ipynb

When the notebook is selected on the Files pane, some options will appear at the top, as Duplicate, Shutdown and Delete. Duplicate will create a copy of your notebook, Shutdown stops a running notebook and Delete erases it.

Working With the Jupyter Notebook

A Jupyter notebook is built of cells stacked on top of each other. These cells can have two types, code and markdown.

Detail of a Jupyter notebook cell
Detail of a Jupyter notebook cell

This is a cell of type code. You can start to write Python 3 code right away inside it. As an example, writing 1 + 2 and pressing ctrl + enter, you will see the result right underneath it.

Detail of a notebook's cell showing the result of 1 + 2
Detail of a notebook’s cell showing the result of 1 + 2

If you use shift + enter instead of ctrl + enter it will show the results and automatically place your cursor on a new code cell and you can just keep coding.

To change a cell type from code to markdown, just use the menu on the top or the keys: m to change code to markdown and y to go from markdown to code.

Detail of the menu to change cell type on a Jupyter notebook
Detail of the menu to change cell type on a Jupyter notebook

Once in markdown mode, you can start typing texts using a markdown similar to the one used on a README file for Github. You can find a really good reference for markdown from IBM Here.

Jupyter notebook cell with markdown
Notebook cell with markdown

When we execute this cell, it will change to a “compiled” text. To go back and edit it, double click on the cell to convert it back to markdown.

Text formatted from markdown on a notebook cell
Text formatted from markdown on a notebook cell

Editing and Command Modes

When you select a cell it can be in two modes, editing and command. Clicking a cell will make it enter command mode, this is represented by the blue border at the left of the cell. If you click on the edit area it will change into edit mode and the left border will be green.

Pressing the esc key on editing mode will take you to command mode.

You can check all shortcuts available on Jupyter Notebook by navigating to Help → Keyboard Shortcuts on the menu.

Checkpoints

Instead of saving the notebook at the latest version, you can create checkpoints with the current notebook state and then come back to the same state later. Checkpoints are very useful to test new things without the fear of ruining the work you have already done.

To create a checkpoint go to Save and Checkpoint at the File menu. To revert to a saved checkpoint, click on Revert to Checkpoint on the same menu.

Conclusion

Now you have learned how to use the Jupyter Notebooks to run Python code. Commonly used with libraries like numpy, pandas (you can check my intro to Pandas 😊) and matplotlib, notebooks can come handy to test and validate lots of other projects. An interesting use can be validating endpoints on a API by calling the internal methods directly on a notebook.

I hope this tutorial helps you get into the world of notebooks. Please, ask any questions on the comments and I will answer as fast as humanly possible.

If you found value on this article and want to read more on Python, please follow me :)

--

--

Pedro Henrique

I am a Brazilian data engineer working out of Stockholm. when I am not thinking on ways to move data around, I enjoy riding motorcycles and reading books.