How to use Jupyter Notebook for Python Programming

Jupyter Notebook is one of the best tool for a Data Science enthusiast. Learn how to use it to maker your programming more efficient and interactive.

One of the many benefits of programming in Python is that you can run them in small scripts in notebooks such as the Jupyter Notebook. This is what makes Python great for Data Science and Research. since you have to edit and test your code over and over again so instead of running the entire code again, you can simply run the small chunk that you altered.

If you are absolutely new to python it would be better if you started here.

In this article, we will be discussing the benefits of writing code in a Notebook and looking at some of the additional features that a Notebook has to offer.

Installing Jupyter Notebook

Jupyter Notebook comes as part of the many packages that Anaconda has to offer. Anaconda is a package manager that offers several tools for Data Analysis and Data Science. We’ll be starting this article off by downloading and installing Anaconda.

Logo of the anaconda distribution

You can download Anaconda from the following link.

Download Anaconda

Once you have the installer ready, go with the flow. Install it like you would install any other software. Once it is installed just go to your search bar and type ‘Anaconda‘ and the installed application should show up as ‘Anaconda Navigator. Click on it and let it load (Warning: The Anaconda GUI can take some time to load).

Once loaded you will see the following interface. (Yours may look a little different depending on what version you install).

Anaconda Navigator interface which allows you to launch any of its offered software.
Anaconda Interface

Here you can see that multiple apps come installed with it such as Jupyter Notebook, Jupyter Lab, Spyder as well as a qt console which provides a nicer interface for python command-line programming than your usual command line.

What we need here is only the Jupyter Notebook. But we will not be running it from here.

Running the Jupyter Notebook

Anaconda comes with so many tools, but we only require the ‘Jupyter Notebook’ for this article. So instead of opening the Anaconda Navigator every time (Which takes a lot of time to load), we will use a shorter method.

Type ‘Anaconda prompt’ in the search bar, you will see a window open that looks very much like the command prompt.

The Anaconda prompt which can be used to launch any of the software included with the anaconda distribution withiest opening the navigator.
The anaconda prompt

Now in the console type ‘jupyter notebook’.

The jupyter notebook server running on your local machine
Jupyter notebook hosted on your local system.

Once you hit enter, a local server will be created on your computer on which the notebook will be hosted.

The notebook interface should automatically open up in your web browser but in case it does not all you have to do is copy the provided link and paste it into your search bar.

The interface looks like this:

Jupyter Notebook navigation window interface
Jupyter notebook interface

The interface will open in the directory which was set in your Anaconda prompt so you might want to change that before launching the Jupyter Notebook.

Now, this is just the directories interface, we haven’t actually created the notebook yet. To do that click on “New” -> “Python3” The new notebook is titled “untitled” by default. Let’s change this to something more appropriate. Just click on the name to change it.

Renaming the Notebook
Renaming the Notebook

Once your notebook file will be created you will notice a subtle difference between this notebook file and a normal python code file. Python code files end with extension ‘.py’ whereas in the notebook file we will have the extension as ‘.ipynb‘. My guess is this stands for Interactive Python Notebook, I haven’t really checked, if you have then do comment below.

I will change the name to “Test_notebook”. Now let’s write some code here. The block that you see on the screen is where the code will go. With the code written, press the “run” button on the toolbar above or press ‘shift + f5’ to run the cell block which is highlighted.

Running a cell block of python code in Jupyter Notebook
Running a cell block in the notebook

Using multiple cells for coding

You can see that the output of that particular cell is printed right below it. The advantage of using a notebook is that I can run smaller chunks of code independently. Any variable defined in a particular cell will also be accessible in another cell. I cannot stress enough how advantageous this cell block structure is. The below example may explain it better.

Python code spread over multiple blocks
Code spread over multiple blocks

As you can see the variables ‘a’ and ‘b’ are defined in different cells and are still printed out when cell no.3 is run. This is because the entire notebook has a shared memory and you should treat it as a single python file.

Now what I can do here is go back to cell 1, change the value of ‘a’ and rerun the cell, and ‘a’ would have a different value. Now I only need to run cell 3 to print this value (cell 2 does not need to be run again). This is efficient since you don’t have to run the entire code after making a small change, like in a normal IDE.

Creating a Notebook: Markdown cells

There’s a reason that the name ‘Jupyter Notebook‘ has the term ‘Notebook‘ in it. It’s because you literally create a notebook out of your python code.

Jupyter allows you to create amazing documentation of your code using Markdown language. You can write working and explanations of your code in separate markdown cells. This is a particularly helpful feature when you have to share your notebook with your colleagues or co-workers. If you have written down the ”notes” properly then it will help them in understanding your work much better.

You can specify a cell as a markdown cell by selecting from the dropdown in the toolbar at the top.

Highlighting a markdown cell for documentation
Highlighting a markdown cell for documentation

Once highlighted, you can write normal text in a markdown cell.

Text written in a markdown cell in jupyter notebook

The procedure is the same. Write some text in the markdown cell and then run the cell the same as you would for a code cell. Jupyter would interpret the markdown language and display text according to the markdown syntax.

There are many features in Markdown e.g. you can write headings in the notebook and create lists.

## This is a heading Below is a list - point number 1 - point number 2
Code language: PHP (php)

Copy the above text into a markdown cell and run it. you will see the output shown below.

demonstration of different features of markdown language

This markdown functionality provides certain structuring to the code which makes it very easier to read and gives a nice look to the notebook overall. You can explore the markdown capabilities by reading its documentation.

“““`Additional Functionality: Magic Functions

Photo by Tima Miroshnichenko from Pexels
Photo by Tima Miroshnichenko from Pexels

Jupyter Notebook has a set of built-in functions called the magic functions. These functions are invoked using the percentage symbol ‘%’ and enhance the usability of the notebook. 

You can find the entire list of magic functions in the official documentation here.

For this article, I will only discuss which I found most useful.

1. Timing functions

There are certain timing functions that you can use in Jupyter Notebook to record the execution time of a certain block or line of code. To find the execution time of an entire cell you place the ‘%time’ command at the top of the cell.

%%time #will output the time taken to execute this entire cell print("Execution Started...") for i in range(1000000): a = i print("Execution complete")
Code language: PHP (php)
Output cell with Execution time printed at the bottom.
Output cell

We can see that this cell took 68.5 ms to execute on my system. 

We can also specify to only find the execution time of 1 statement in the entire cell. We need to change the magic function to ‘%time’ 

#added some extra statements to be executed a = 2 b = a print("Execution Started...") %time for i in range(1000000):a = i #time for this statement will be recoreded print("Execution complete")
Code language: PHP (php)

the ‘%time’ looks for an executable statement exactly in front of it and records its execution time.

Execution time of only one statement was recorded in the jupyter notebook cell
Execution time of only one statement was recorded

The order in which these lines are printed shows that execution time was only recorded for the for loop and was printed out and then the final print statement was executed.

Since execution times can vary every time the code is run, it is a good practice to take the average of multiple runs. Good thing Jupyter also has a magic function for this. Just add ‘%%timeit’ at the top of the cell.

%%timeit #entire cell will be executed multiple times #added some extra statements to be executed a = 2 b = a print("Running cell..") for i in range(1000000):a = i
Code language: PHP (php)
The jupyter notebook cell was executed multiple times
Multiple Executions

The multiple prints in the output tell us that the cell was executed multiple times and an average was provided at the end.

2. Interactive plots in Matplotlib

A very big challenge in data visualization in MatPlotlib is that the visualizations you create are static unless you write some very complex code. 

Well, Jupyter Notebook has got you covered. Run the following command in an empty cell in your Jupyter Notebook; ‘%matplotlib notebook’.

Below is a plot in a Jupyter Notebook without the magic command.

A simple scatter plot created in jupyter notebook using matplotlib
A normal scatter plot in jupyter notebook

And now with the magic command.

%matplotlib notebook
An interactive plot after using the notebook backend in jupyter notebook
An interactive plot with the magic command active

The above plot allows you to zoom into the data points and hover around for a better view. 

The %matplotlib magic command has multiple options. If you use the ‘qt’ option, it allows you even more ways to interact with the plot. Your magic command will look like this now.

%matplotlib qt

NOTE: If Jupyter throws a warning when running the magic command then no need to worry, you just need to restart your kernel and then run the code again.

With the qt backend, the plot opens in a new window that has several new features.

Scatter plot in separate window after using the qt backend
Plot using ‘qt’ backend

You have several options to change the layout, edit the axis and even save the plot as an image file.

Final Thoughts

Though IDE-based programming is the general way for a programmer, using a notebook is very helpful, for data scientists. You can change whatever parameters you like and then rerun only the desired cells. On top of all of this, the added functionality of the notebook such as writing markdown text and magic functions helps in creating an actual notebook that others can read and understand easily.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Python for Absolute Beginners in 3 easy steps

Python for Absolute Beginners in 3 easy steps

Your first step in getting started with Python

Regex 101 for Python Data Science
Understanding regex with python

Regex 101 for Python Data Science

Regex are an NLP technique that provide structure and format to your text

You May Also Like