How to use Jupyter Notebooks: A Complete Tutorial
What is Jupyter Notebook
Jupyter Notebooks are a powerful way to write and iterate on your Python code for data analysis. Rather than writing and re-writing an entire program, Jupyter Notebooks allow you to write code in separate blocks (or “cells”) and run each block of code individually. Then, if you need to make a change, you can go back and make your edit and rerun the program again, all in the same window.
Jupyter Notebook is built off of IPython, an interactive way of running Python code in the terminal using the REPL model (Read-Eval-Print-Loop). The IPython Kernel runs the computations and communicates with the Jupyter Notebook front-end interface. It also allows Jupyter Notebook to support multiple languages. Jupyter Notebooks extend IPython through additional features, like storing your code and output, and allowing you to keep markdown notes.
If you’d rather watch a video instead of read an article, please watch the following instructions on how to use a Jupyter Notebook. They cover the same information.
Getting Started Off-Platform for Data Science
Learn how to setup Jupyter Notebooks and PostGRESQL and run data science projects on your own computer locally!Try it for freeInstalling Jupyter Notebook
There are two common ways to install Jupyter Notebook:
- Using the Anaconda distribution or
- Installing via
pip
.
The Anaconda distribution is the most beginner-friendly option as it comes with Jupyter Notebook pre-installed, along with Python and many useful packages like NumPy, pandas, and Matplotlib. To install it, visit the Anaconda website and download the installer for your operating system, and follow the setup instructions. Once installed, Jupyter will be available from the Anaconda Navigator or directly from the terminal.
If you already have Python installed and prefer to manage packages manually, you can install Jupyter using pip
. Open your terminal or command prompt and enter the following command:
pip install notebook
This will install Jupyter Notebook and all its dependencies, so you can start using it with your existing Python setup.
How to launch a Jupyter Notebook from terminal
To launch a Jupyter notebook, open your terminal and navigate to the directory where you would like to save your notebook. Then type the command jupyter notebook
and the program will instantiate a local server at localhost:8888
(or another specified port).
A browser window should immediately pop up with the Jupyter Notebook interface, otherwise, you can use the address it gives you. The notebooks have a unique token since the software uses pre-built Docker containers to put notebooks on their own unique path. To stop the server and shut down the kernel from the terminal, hit Ctrl+C twice.
Understanding the Jupyter Notebook interface
Now you’re in the Jupyter Notebook interface, and you can see all of the files in your current directory. All Jupyter Notebooks are identifiable by the notebook icon next to their name. If you already have a Jupyter Notebook in your current directory that you want to view, find it in your files list and click it to open.
How to create a new notebook in Jupyter
To create a new notebook, go to New and select Notebook - Python 3. If you have other Jupyter Notebooks on your system that you want to use, you can click Upload and navigate to that particular file.
Notebooks currently running will have a green icon, while non-running ones will be grey. To find all currently running notebooks, click on the Running tab to see a list.
How to use cells, markdown, and code execution in Jupyter Notebooks
When you open a new Jupyter notebook, you’ll notice that it contains a cell.
Cells are how notebooks are structured and are the areas where you write your code. To run a piece of code, click on the cell to select it, then press Shift+Enter or press the play button in the toolbar above. Additionally, the Cell dropdown menu has several options to run cells, including running one cell at a time or running all cells at once.
After you run a cell, the output of the cell’s code will appear in the space below. To stop running a piece of code, press the stop button.
To create new cells, use the plus (+) button in the toolbar or hit Shift+Enter on the last cell in the Notebook. To cut, copy, delete, or just generally edit cells - select the cell you want to modify and go to the Edit button in the navigation bar to see your options.
In addition to running lines of code, you can also include text-only cells that use Markdown to format and organize your notebooks.
When you create a new cell, it will default to being a Code cell. To create a cell that uses markdown, click on the Cell menu from the navigation bar, scroll down to Cell Type, and choose Markdown.
Occasionally, you might need to restart the kernel. Head to the Kernel dropdown and hit Restart. To shut down a kernel, you can click Shutdown, which will have a dialogue process asking if that’s what you would like to do. To force an immediate shutdown, go to the File dropdown and click Close and Halt, and the browser window will close itself. Restarting and shutting down kernels will affect your variables, so be careful.
In the Help dropdown, you’ll find useful information such as keyboard shortcuts as well as links to different documentation for modules such as Numpy, SciPy, and Matplotlib.
The toolbar has several shortcut buttons for popular actions. From left to right: save, add a new cell, cut selected cells, copy selected cells, paste cells below, move selected cells up, move selected cells down, run, interrupt the kernel, restart the kernel, a dropdown that allows you to change the cell type, and a shortcut to open the command palette.
Jupyter Notebook files are saved as you go. They will exist in your directory as a JSON file with the extension .ipynb
. You can also export Jupyter Notebooks in other formats, such as HTML. To do so, go to the File menu, scroll down to Download as, and select the type of file you’re looking for. A pop-up will appear asking where you would like this new file to download. Once you’ve navigated to the appropriate directory, click Save and Checkpoint.
Conclusion
Jupyter Notebooks offer an intuitive and flexible environment for writing, testing, and organizing Python code. Whether you’re running code in cells, adding markdown notes, or managing kernels, Jupyter makes it easy to experiment and build projects efficiently—all in one interface.
To practice using Jupyter Notebooks with real data, explore Codecademy’s Learn Python for Data Science course. It’s a great way to apply your skills and work with data directly inside notebooks.
Frequently asked questions
1. What is Jupyter Notebook mainly used for?
Jupyter Notebook is mainly used for writing and running code interactively. It’s popular in data science and machine learning for combining code, visuals, and text in one workspace.
2. How to run a Python file in Jupyter Notebook?
To run a Python file (.py
) in Jupyter Notebook, you can use the %run
magic command. First, make sure the .py
file is in the same directory as your notebook. Then, in a code cell, type:
%run your_script.py
3. How do I import code into Jupyter?
You can import functions, classes, or variables from other Python files using the standard import
statement. For example, if you have a file named helper.py
, you can write:
import helper
Or use from helper import function_name
to import specific elements. Make sure the file is in the same directory or in your Python path so Jupyter can access it.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Writing and Debugging Code in Jupyter
A walkthrough of Jupyter Notebook's most useful features. - Article
Introducing Jupyter Notebook
What is Jupyter Notebook? - Article
How To Install Jupyter Notebook on Mac and Windows
Learn how to install Jupyter Notebook using conda with Anaconda or Miniconda. Step-by-step installation guide for Windows and Mac users.
Learn more on Codecademy
- Free course
Getting Started Off-Platform for Data Science
Learn how to setup Jupyter Notebooks and PostGRESQL and run data science projects on your own computer locally!Beginner Friendly1 hour - Free course
Getting Started with Python for Data Science
Work hands-on with real datasets while learning Python for data science.Beginner Friendly7 hours - Skill path
Data and Programming Foundations for AI
Learn the coding, data science, and math you need to get started as a Machine Learning or AI engineer.Includes 9 CoursesWith CertificateBeginner Friendly39 hours