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.
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.
py get-pip.py
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.
jupyter-lab
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.
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
}
animals.keys()
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.
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.
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
