Introducing Jupyter Notebook
What are Jupyter Notebook & IPython?
Jupyter Notebook is a popular product that data scientists, analysts, and researchers use to analyze, store, and share work. Likewise, IPython is an indispensable tool for conducting your data research and analysis. Let’s dive deep into Jupyter Notebooks and IPython to understand what makes them stand out from your basic Python Interpreter.
Philosophy of a Notebook
Whether you are doing research from a laboratory bench or a computer at your desk, keeping a notebook allows you to keep track of your work so you may revisit your process days, months, or years in the future. Recording detailed notes can include information about when you conducted this research, which variables and data you used, which steps and methods were used, and what observations you may have had throughout the process. Reproducibility is crucial for academics as well as those in practice. By keeping a notebook, you and others can rerun work to get similar results and hopefully improve on the work.
What is Jupyter Notebook?
Jupyter Notebook is 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.
In addition to Python code, Jupyter Notebook allows you to mix other programming languages and rich text elements like mathematical equations, charts, hyperlinks, and stylized text. This allows users to create polished, shareable outputs from analysis that may have taken multiple programs to accomplish previously.
How Does Jupyter Notebook work?
Jupyter Notebook is an interactive web-based application. In the diagram below, you will see that the user interacts with the notebook through a browser such as Firefox, Chrome, or Safari. The notebook server is connected to the kernel (often using IPython to execute Python code) and the document you are working on is being edited through the notebook file. The following are the two main components that make up Jupyter Notebook:
- The kernel running the code.
- The notebook application that combines the server and front-end through the browser for the user to interact with.
What is IPython?
IPython is a Python interpreter that allows you to run Python code within your terminal. Although this is helpful, the Python interpreter is very bare when it comes to handy helpers for coders. The IPython interpreter goes further by providing syntax highlighting, tab completion, proper indentation, and several other important features that an extended code editor provides. This is what makes IPython a welcoming alternative Python interpreter and interactive shell used for computing in Python. Let’s get more acquainted with IPython and a few of its features.
Make sure that you install IPython.
What Are Some Features of IPython?
Here are a handful of features that make IPython much easier to use over its default counterpart:
Run Native Shell Commands
When you run an interpreter, the interpreter typically has its own built-in commands. These built-in commands often collide with native shell commands.
For example, if you were to start the Python interpreter (using
python) and then type
cd after the interpreter loads, an error would appear in your terminal. This error occurs because the Python interpreter does not recognize the command. The command
cd is native to your computer’s terminal, but not to the Python interpreter. IPython includes support for native shell commands like
ls, command history, and more.
One of the first things you’ll notice about IPython is that it provides syntax highlighting, meaning it uses color to differentiate parts of Python code. Type
x = 10 into the terminal and notice how IPython highlights the code with different colors. This syntax highlighting makes code much easier to read in the terminal.
If you’ve used Python before, you know that it cares a lot about whitespace and indentation. IPython recognizes this and automatically provides you with proper indentation when you type Python code in the interpreter. Let’s take a look at an example.
Start the IPython interpreter by typing
ipython in your terminal.
Next, type the following code and press Enter (or Return) on your keyboard:
numbers = [1,2,3,4,5]
Now type this line in the terminal and press Enter (or Return):
for x in numbers:
Notice that after you press Enter (or Return) on your keyboard, IPython automatically provides the required indentation (four spaces) on the next line of code. On the next line, type the following code:
To run the code, press Enter (or Return) twice . (The second Enter informs IPython that you are typing code and instructs IPython to execute the code.) Voila! You should see the following output:
IPython also provides tab completion. Let’s take a look at an example using Python’s
str module. As a refresher, recall that the
str module provides some useful methods you can use on strings.
Start IPython in your terminal. Type the following code and then press Tab on your keyboard:
After pressing tab, you should see a list of methods supported by the
You can use the up and down arrow keys on your keyboard to navigate through the methods and select the one you’d like to use, or you can begin typing the name of the method you want to use and complete it by using Tab. This a vast improvement over the default interpreter!
Tab autocompletion is useful because it provides you with the list of all possible methods a specific module contains. With such a vast array of options, however, you might wonder: “What does this method do?” Once again, IPython to the rescue! Type the following command into IPython and press Enter (or Return) on your keyboard:
You should see IPython provide you with the documentation for that method.
You can use
? at the end of nearly any command in IPython if you want more information on the command (or method, variable, etc.).
Pasting Blocks of Code
Last, but certainly not least, another useful feature of IPython is the ability to paste in large amounts of Python code directly into the interpreter. You can grab any block of Python code, paste it into IPython, and the result should be properly indented code in the IPython interpreter. Neat! These are only a handful of features that IPython provides. If you’d like to learn more about IPython, we recommend the following resource:
IPython: Interactive Computing
Jupyter Notebook and IPython provides improvements over the default Python interpreter. These improvements include syntax highlighting, proper indentation, documentation, and much more. IPython can also power Jupyter notebooks as a kernel to create reports that contain live code, charts, and more. If you’re serious about using Python for computing and data analysis, Jupyter Notebook is a phenomenal tool for you to use.