A tour of Python + Jupyter Notebooks

[This article was first published on George J. Mount, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In an earlier post I offered a tour of RStudio, the popular integrated development environment and my preferred interface for teaching R. With Python, I favor Jupyter notebooks so figured a similar tour would be worth it.

This post lays out the basics of Jupyter, how to navigate the interface, and how to execute Python code with it.

What are Jupyter notebooks?

Jupyter is a web-based application used to create interactive files containing code, text and other assets. It is developed and maintained by the open source Project Jupyter. Initially conceived as a more interactive way to work with Python code, the project has since been expanded to scientific computing more generally.

How do you run then?

Python and Jupyter are two different things: the code and an interface for executing the code, respectively. You can download them both, along with some other goodies, with Anaconda Individual, a free install.

To learn more about installing and navigating Jupyter from your computer, check out my book Advancing into Analytics.

Alternatively, you can run a session of Jupyter on the cloud for free here. It will take a moment or two to load; eventually you should see a “Home Page” listing the contents of the repository. Create a new Jupyter notebook here by selecting New > Python 3:

You will now see a blank Jupyter notebook; now let’s review its four elements.

The elements of Jupyter

Elements of a Jupyter notebook

1. Notebook name

This area displays the notebook name. The file extension for any Jupyter notebook is ipynb, or IPython Notebook. To rename the notebook, you can click and type right over this area.

2. Menu bar

Like most applications, Jupyter contains a menu bar containing some helpful features. A couple particularly to be aware of:

File

Here you can duplicate and create new notebooks. You don’t have to worry so much about saving, as by default Jupyter notebooks are autosaved every two minutes. You can create checkpoints if there are points in your work that you’d like to save a copy of. You can also download a copy of your work in several common file formats such as Markdown (md) or Python scripts (py).

Kernel

The kernel is what Jupyter uses “under the hood” to execute and return the Python code. To be more precise, Jupyter offers a kernel in many programming languages, including JavaScript, SAS and R. (Fun fact: Jupyter is a portmanteau of the Julia, Python and R languages).

If you’re having problems with your notebook, restarting the kernel is similar to restarting your computer — hackneyed advice, but it often works. Be careful, though: once you restart the kernel you’ll lose everything in memory during the session, such as any datasets you’ve imported.

3. Toolbar

This is equivalent to Excel’s Quick Access Toolbar and contains icons for common operations; most of these have to do with cells which are the predominant area of the notebook.

4. Cells

The content of Jupyter notebooks are arranged in modular cells which can be added, deleted and rearranged. Four cell types are offered; we’ll focus on the two most common: Markdown and Code.

Markdown

To make your first cell Markdown, click the dropdown in the menu bar and select Markdown. This is a plain-text markup language; to learn more about it head to the menu and Help > Markdown.

Choose Markdown from menu Jupyter cells

Now, type or paste the following text into your Markdown block:

# Big Header 1
## Smaller Header 2
### Even smaller headers
#### Still more
*Using one asterisk renders italics*
**Using two asterisks renders bold**
- Use dashes to...
- Make bullet lists
Refer to code without running it as `fixed-width text`

Looks pretty weird, right? Just wait a second — you can render this Markdown by selecting Run from the menu. It should now look like this:

Rendered Markdown in Jupyter

The rendered text wouldn’t look out of place in a word processing doc. With Markdown you can even include photos, hyperlinks and more. Markdown blocks also render HTML for even more text customization. This blend of text and other assets with code is one of Jupyter’s most celebrated features. But you’re probably not here to get a lesson in Markdown but instead to learn the basics of working with Python in a notebook.

Code

When you ran the Markdown cell, a new cell was inserted immediately underneath. Change the menu dropdown to Code; let’s look into the basics of running Python code in Jupyter (from, well, Jupyter):

Jupyter notebooks provide a powerful and appealing medium for working with code, which is often (but not always) Python. This post gave you just enough know-how to work with both.

For more, I encourage you to kick the tires on your Jupyter notebook (You’re not going to break anything… you can even ransack the cloud-based link I gave you, no consequences!). In particular, check out the Help menu for links to various Python tutorials and an interactive tour of the Jupyter interface. Jupyter also contains an abundance of keyboard shortcuts — you can view them all from a window off the Help menu.

Advancing into Analytics Cover Image

One could easily write a book about working with Python and Jupyter… wait for it…

Keep learning about Python & Jupyter notebooks… get your copy of my book Advancing into Analytics: From Excel to Python and R.

To leave a comment for the author, please follow the link and comment on their blog: George J. Mount.

Want to share your content on python-bloggers? click here.