How to run a Python program in Jupyter Notebook – with example

  • Reading time:21 mins read

How to run a Python program in Jupyter Notebook

Jupyter is an open-source interactive development environment that allows developers to create and share documents containing code, visualizations and text.

Jupyter notebook is highly popular in the data science field since it allows developers to bundle data, code and visualizations into a single notebook hence easy to comprehend. In this article, you’ll learn how to run a Python program in Jupyter Notebook.



Its intuitive user interface allows users to easily create, edit and run notebooks as a web application on any browser.

Using the Python kernel; a computational engine that runs code that is typed within the notebooks, code is executed and the result neatly displayed immediately after each cell.

Installing Jupyter Notebook

Before we can get started with installing Jupyter we need to make sure that Python is installed.  Jupyter installation requires Python 3.3 or greater, or Python 2.7, these versions can be found and installed from the official python website  


how to run a python program in Jupyter Notebook  

So to get Jupyter installed we first need to visit the official Jupyter website. As of today, Jupyter offers two main products: the Jupyter Notebook and the JupyterLab.

JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data while Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. 

While on this window we need to click the Install option on the navigation bar.    



How to run a Python program in Jupyter Notebook – Installation

There are two main ways that we can use to install Jupyter; we can use Conda which is an open-source package management system for installing multiple versions of software packages and other dependencies. On the other hand, we can also use PIP which is equally a package installer.

Now to install Jupyter using PIP,  we first need to have PIP installed by running the command below in the terminal.  




However, we need to note that PIP is automatically installed with Python 2.7. 9+ and Python 3.4+. Once PIP is in place we can now install Jupyter by typing the command below in the command prompt and pressing the enter button.  


pip install jupyterlab


We have now successfully installed Jupyter in our windows system. If you’re running Jupyter on a different operating system, refer to the official Jupyter website for the correct command to install Jupyter. 

How to run a Python program in Jupyter Notebook – Launching JupyterLab

To launch JupyterLab, we need to type the command below in the command prompt and press the enter button.

This command is going to start the local server so that we can access Jupyter using the browser. Jupyter may also automatically open after running the command below in the command prompt.  




If Jupyter does not automatically open in your browser, please go ahead and copy one of the URLs at the bottom of the text generated after running the launch command.

Paste it on your favourite browser and press the enter button. The URLs are similar to the ones shown below.  



This will open a window that is shown below in the browser. In this case, JupyterLab has been set to a dark theme, however, it may also open in white mode which can be changed under the setting menu.  



To proceed we need to click on Python 3 which is the first tab on this window. 



This will open a new Python notebook which is a new ipynb file named untitled since we’re yet to save it. So this gives us access to all the cells where we can start to write our Python code.

How to run a Python program in Jupyter Notebook – Coding

Suppose we create a simple variable x and initialize it with a value of 5. To run the variable we need to hit the run button.  


how to run a Python program in Jupyter Notebook


Now that the variable has been created in the notebook, we can access it by simply typing the name of the variable in any of the cells. As shown below.  



We can also delete cells by right-clicking on the cell that we intend to delete and selecting the delete option.  



Besides this, we can also write codes that span over more than one line and still run them in the same way. For instance, we can create a list that spans over multiple lines.



Now using a for loop we can also iterate over the list as we would in a normal IDE such as VS Code and the code works just fine. In this case, the code for the for loop need not be in the same cell as the list.


How to run a Python program in Jupyter Notebook with the Matplotlib library

As we had initially mentioned, Jupyter is widely used and loved in the field of data science due to its efficiency when working with data.

It’s particularly good at displaying graphs or performing any sort of visualization. To illustrate this in the cells below we are going to create some sample data and visualize it using matplotlib which is a common visualization library in Python.

Matplotlib can be downloaded using the PIP package manager by running the command below in the command prompt.  


pip install matplotlib

How to run a Python program in Jupyter Notebook – Dictionaries

And now we’re going to create a really basic dictionary that will display the respective number of animals.

In Python, we can access both keys and values of a dictionary using the keys() and values() methods respectively. Here is how we can access the keys of the dictionary.


animals = {
'elephant': 15;
'lion': 25,
'shark': 4,
'monkey': 12


We can also access the values of the dictionary as shown below.  



Now using the keys and the values as the x and y-axis respectively, alongside the matplotlib library we can create a bar graph showing the relationship between these two entities.  


How to run a Python program in Jupyter Notebook


However, we first need to import the matplotlib library at the top of our code as an alias so that it is easier to use it within our code.

Once that is done we can now initialize x and y before calling the bar function that will plot the two. As shown in the code below.  


How to run a Python program in Jupyter Notebook – Cells

Another advantage of using Jupyter is that even if we make a change in the code we need not worry since we can just rerun the only cell that we have made changes to.

For instance, in the example above we can change the number of sharks to 15 and rerun the cell again.  



This is how to run a Python program in Jupyter Notebook. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.