Skip to main content
HomeTutorialsPython

How to Use Jupyter Notebooks: The Ultimate Guide

This article covers what Notebooks are and why you should use them. We also delve into hosted notebooks, which facilitate sharing and collaboration. This article also covers tips, tricks, and keyboard shortcuts.
Updated Mar 2023  · 25 min read

Jupyter and the notebook

Jupyter notebooks are documents for technical and data science content. This tutorial provides an overview of Jupyter notebooks, their components, and how to use them. 

We will explore notebooks using DataLab, a hosted notebook service that provides all the functionality of Jupyter notebooks, along with functionality for connecting to databases, real-time collaboration, and publishing your work. 

This tutorial assumes that you have used a data science programming language before, such as Python, SQL, R, or Julia.

Overview of notebooks

Notebooks combine computer code (such as Python, SQL, or R), the output from running the code, and rich text elements (formatting, tables, figures, equations, links, etc.) in a single document. 

The key benefit of notebooks is the ability to include commentary with your code. That means that you can avoid the error-prone process of copying and pasting analysis results into a separate report. Instead, you simply mix your analysis with the report text in the notebook.

Who should use Jupyter Notebooks?

Jupyter Notebooks are primarily used by data professionals, particularly data analysts and data scientists. According to the Kaggle Survey 2022 results, Jupyter Notebooks are the most popular data science IDE, used by over 80% of respondents. 

Types of Jupyter Notebook

There are two main types of Jupyter Notebook; hosted and local notebooks. DataCamp provides DataLab, a hosted Jupyter Notebook that we will use for the majority of this tutorial. DataLab is an excellent option for learners and professionals who do not want to set up a local environment. 

Except where noted, the functionality described in this tutorial will work on other Jupyter notebook versions. If you prefer to use a local environment, you can install Jupyter Notebook on your machine using our Installing Jupyter Notebook tutorial. Marcus Schanta maintains a list of other hosted notebook platforms.

Components of a notebook

A Jupyter Notebook consists of three main components: cells, a runtime environment, and a file system.

 Cells are the individual units of the notebook, and they can contain either text or code:

  • Text cells are used to write narrative text and include images, links, and equations.
    • Text cells are written in Markdown, a simple markup language.
  • Code cells are used to write and execute code. 
    • The output from code cells will be displayed directly below the code cell. 
  • SQL cells (DataLab only) are used to execute SQL queries, which means you can easily retrieve data from a database.
  • Chart cells (DataLab only) can be used to create visualizations and quickly visualize Pandas dataframes.

The runtime environment is responsible for executing the code in the notebook. The runtime environment can be configured to support different languages, including Python, R or SQL.

The filesystem allows you to upload, store, and download data files, code files, and outputs from your analysis.

Command mode and edit mode

Jupyter notebooks have two different modes of interaction: command mode and edit mode. In command mode, you can navigate between cells, add and delete cells, and change the cell type. In edit mode, you can edit the contents of a cell.

In order to enter command mode, you can either press Escape or click outside a cell. To enter edit mode, you can press Enter or click inside a cell.

In DataLab, you can click the ‘Add Text’ or ‘Add Code’ buttons to add a new cell.

image9.png

Getting help

For Jupyter notebook, you can get help using the documentation or using the option in the menu. In DataLab, help and keyboard shortcuts can be quickly accessed by pressing the help button in the menu.

image14.png

Writing text

Text cells are written in the Markdown markup language, allowing you to easily write and format text. While in edit mode, you can use syntax such as ** ** for bold, or use the buttons, to format your text. 

Here are a few different options:

image11.png

Pressing shift + enter or the ‘View’ button will run the cell, giving the following result.

image17.png

  • Lines beginning # are a top-level header. Start with ## for a second-level header, ### for a third-level header, and so on.
  • Surround text in ** to make it bold, __ to make it italic, and ` to make it code formatted.
  • Start consecutive lines with - to make them into a bulleted list.
  • Start lines with numbers followed by a period to make them into a numbered list.
  • Hyperlinks are written in two parts. The text to display is surrounded by square brackets, then the url is surrounded by parentheses.

Writing and running code

Pressing ‘Add Code’ or entering a command with (escape) and pressing ‘B’ will add a new code block.

image3.png

Write code in the cell just as you would in a script.

image12.png

Pressing Run or CTRL/CMD+Enter runs the code and displays its output.

image2.png

Reading and writing files

Pressing ‘Browse and upload files’ on the left-hand menu brings up the file system, and pressing the ‘plus’ will allow you to upload a file from your local machine. Below, we have uploaded a simple text file called hello_world.txt.

image13.pngimage19.png
We can use the following code to open the file, add some text, then save a new file.

image7.png
You’ll now see the new file in the file system, and it will contain our updates.

image15.png
Working with the File System

We have shown how to upload, update and create a new file. To download the new file, press the three dots in the file system and hit download.

image18.png
The plus button used to create new files can also be used to create fresh notebooks, which will have no cells or output. 

image4.png

Commanding cells

You can quickly reorder cells with the move up and move down buttons, as shown in the image below. 

image20.png

This will reorder your code. (Note that your code may break if you try and run it in the wrong order!)

The Hide Code button will collapse and hide the code; this is useful for very long code blocks that you aren’t currently working on. It is also useful if the readers of your analysis don't care about the technical details and only want to see the results.

image6.png
Similarly, the Hide Output button allows you to hide long outputs. 

image10.pngThese buttons can also be used together to hide both code and output.

Publishing reports (DataLab only)

DataLab allows you to publish your notebooks as publications. This is a great way to showcase your excellent work and collaborate with other data scientists.

You can publish your notebook by pressing the ‘Publish’ button on the side menu. From there, hit publish to share your notebook. It is a good idea to run the notebook from top to bottom before publishing. This helps to check your code and ensures it is readable, as most people will read from top to bottom.

image1.png
Once your notebook has been published, other users can view the publication and comment on individual cells. You can also do the same to others. This is a great way to open up discussion or understand a complex piece of code. Here’s a Workplace example

image5.png
Sharing Jupyter Notebooks (DataLab only)

Sharing workbooks is another useful DataLab-only function. Because the notebook is hosted, you can share a public or private, access-controlled link that the receiver can run themselves. 

This is a fantastic way to collaborate. Data Science is a deep and wide field, meaning no single person is expected to know everything. Data scientists must collaborate to get the best results, whether it’s efficient code, compelling visualizations, or an accurate model. DataLab allows real-time collaboration, where multiple people can edit a notebook at once.

To share your notebook, press the share button on the top right. Here you can copy the link, make the notebook private/public, and set who can access the notebook (if private).

Take it to the next level

Start your data science journey today by signing up for DataLab for free. If you get stuck, the DataLab Documentation is a great place for more information.

Topics

Learn more about Python

Course

Introduction to Python

4 hr
5.6M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

Notebooks for R Users

DataCamp recently migrated RStudio workspaces to DataCamp’s Notebook Editor which features a JupyterLab interface. This post is designed to help R users get started and be productive with JupyterLab.
Filip Schouwenaars's photo

Filip Schouwenaars

11 min

cheat sheet

Jupyter Notebook Cheat Sheet

This Jupyter Notebook cheat sheet will help you to find your way around the well-known Notebook App, a subproject of Project Jupyter.
Karlijn Willems's photo

Karlijn Willems

2 min

tutorial

Getting Started with JupyterLab Tutorial

In this article, we will introduce you to JupyterLab, one of the most popular IDE for data science.
Javier Canales Luna's photo

Javier Canales Luna

7 min

tutorial

Markdown in Jupyter Notebook Tutorial

In this tutorial, you'll learn how to use and write with different markup tags using Jupyter Notebook.

Olivia Smith

9 min

tutorial

Kaggle Datasets Tutorial: Kaggle Notebooks

Learn about Kaggle datasets and notebooks and get a head start on creating your Kaggle profile.
Çağlar Uslu's photo

Çağlar Uslu

7 min

tutorial

Google Colab Tutorial for Data Scientists

A brief guide for navigating Google Colab to carry out data science coding and collaborating with other data scientists.
Bala Priya C's photo

Bala Priya C

12 min

See MoreSee More