Jupyter Notebook Tutorial: How to Use Jupyter Notebooks in 2022
Python is one of the world’s most popular programming languages. A variety of technical domains make use of it, including web development, data science, natural language processing, and machine learning. The reasons for its growth are easy to understand. The language has the perfect balance between power and productivity. Python enables you to build complex, real-world software applications with a terse syntax renowned for its elegance and simplicity. Developers can accomplish a lot with a little code in Python.
If you’re learning Python for the first time, you might be curious about the best text editor or environment in which to learn the language. In this article, we’ll explore why Jupyter Notebooks are a great option for new and experienced developers alike. The Notebook’s design encourages active experimentation by showing you the results of your code immediately. Read on to find out how to get started with this popular technology.
Last Updated November 2023
Analyze data quickly and easily with Python’s powerful pandas library! All datasets included — beginners welcome! | By Boris Paskhaver
Explore CourseWhat is a Jupyter Notebook?
Jupyter Notebook is a development environment for writing and executing Python code. The non-profit, open-source Project Jupyter organization maintains the software. It comprises a sequence of cells, each containing a small code sample or documentation in Markdown. Developers can execute a cell and see its output immediately below the code. The ingenious design creates an instant feedback loop, allowing the programmer to run their code and change it accordingly.
Jupyter Notebook cells also support annotations, audio files, videos, images, interactive diagrams, and more. This is another major benefit of the software; you can tell a story with your code. Readers can see the steps you followed to get to your result.
You can import Python packages like pandas, NumPy, or TensorFlow directly into the Notebook. The figure below shows an example of the pandas data analysis library executing in Jupyter.
How do I get started with Jupyter Notebooks?
The easiest way to get started with Jupyter Notebook is by installing Anaconda. Anaconda is a Python distribution, a bundle of related software. Anaconda installs Jupyter alongside 100+ Python libraries for data analysis, statistics, visualization, and more. You can set everything up with a single installer.
Anaconda is available for Windows, macOS, and Linux machines. Installation is simple. Head to https://www.anaconda.com/products/individual and locate the Graphical Installer for your operating system. Follow the instructions to set up the application on your computer.
Anaconda pre-installs several popular development environments for Python. Once the installation completes, launch the Anaconda Navigator application. From its primary screen, you’ll be able to launch any of these coding environments.
Click on Jupyter Notebook to run the notebook. The application will launch in your default web browser.
The Notebook interface is essentially a file navigator. It is like the Finder app on macOS or the Windows Explorer app on Windows. You can traverse the folders on the computer to your work directory, then create or open a Jupyter Notebook file.
To create a new Notebook, click the New button on the right-hand side of the screen. Select Python 3 from the drop-down list; Jupyter will create a Python notebook. Jupyter Notebook files have a .ipynb extension.
The Notebook displays its title at the top; it is currently “Untitled.” Click the title, and a modal will appear where you can give the notebook a custom name.
What can I do in a Jupyter Notebook?
A Jupyter Notebook consists of cells. There are two primary cell types:
- A code cell holds Python code.
- A Markdown cell holds Markdown code.
Markdown is a formatting language that uses symbols to denote elements like headers, lists, and blockquotes. For example, the # symbol before a piece of text creates a heading. You can use Markdown to provide descriptions, annotations, metadata, or anything that will help a reader understand your work.
When you click into a cell, you can choose its type from a drop-down menu on the top toolbar.
Jupyter Notebook operates in two modes: Edit Mode and Command Mode.
Activate Edit Mode by clicking into any cell. In this mode, the Notebook interprets our keystrokes literally. We can use our keyboard to enter text into a cell.
Let’s execute some sample Python code. You should see a single cell in your newly created Notebook. Click into it and enter a basic mathematical expression like 1 + 1.
Python has not processed our code yet. With your cursor inside the cell, press Shift + Enter to execute the cell. Jupyter will output the result of the evaluation below. It will also create a new cell directly below the executed one.
Notice the [1] to the left of the cell. The number reflects the cell execution order relative to the start of the Notebook. We can execute cells in any order, and we can execute any cell multiple times. The number in square brackets will update accordingly.
We can immediately see the benefits of this trial-and-error approach to development. There is no need to save and re-save files or run Python from the Terminal. The Notebook gives us instantaneous feedback. We observe the results we get and change our code accordingly.
Press the Escape key to enter the Notebook’s second mode, Command Mode. Think of this as a “navigator mode” where you perform Notebook-level operations. In Command Mode, you can create cells, copy and paste cells, delete cells, etc.
The Notebook includes several helpful keyboard shortcuts in Command Mode to reduce notebook clicks. Some of my favorites include:
- Press a to create a cell above the currently selected cell.
- Press b to create a cell below the currently selected cell.
- Press y to convert a cell to a code cell.
- Press m to convert a cell to a Markdown cell.
- Press Shift + Enter to run the current cell and move to the next cell.
- Press Ctrl + Enter to run the cell and stay in it.
You can access more advanced options on the top-level navigation menu. It includes options for copying and pasting cells, exporting the Notebook to various types (PDF, HTML, etc.), altering cell presentation, and more.
If the notebook ever becomes unresponsive, the Kernel > Restart command on the top-level navigation menu restarts the kernel. The kernel is the “listener” that is processing and responding to our cell executions. Restarting the kernel is equivalent to closing and relaunching the Jupyter Notebook.
Once you’re done for the day, save the Notebook (Ctrl + S on Windows, Cmd + S on a Mac), close the browser, and shut down Anaconda Navigator. This ends the running Jupyter process.
Summary: Where do I go from here?
As you can see, Anaconda + Jupyter Notebook offers a powerful and easy way to get started with Python.
With Jupyter, you can write your Python code and see its results instantaneously. You can couple the code with Markdown cells to leave additional comments and documentation for your work. I like to use Markdown for notes, links, explanations, even jokes that help my understanding the next time I read the code. Jupyter was a big factor in helping reduce friction when I was first learning the Python programming language.
Many Python courses on Udemy use the Jupyter Notebook environment. I hope it proves helpful in your future studies!
Recommended Articles
Top courses in Python
Python students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.