Writing and Debugging Code in Jupyter
Let’s walk through the process of working with a Jupyter Notebook.
There are multiple ways to open a Jupyter Notebook. If you have Anaconda, you can continue to launch from the Navigator like we did when we installed Jupyter Notebook. Note that Jupyter Notebook works best on Chrome, Safari and Firefox browsers.
If you would prefer, here is a video walkthrough.
If you have Miniconda or just want to use the Command Line, open the BASH/zsh Shell (Mac) or Anaconda Prompt (Windows) and launch a Jupyter Notebook with the command:
A Jupyter Notebook interface will appear in your default browser. (If you don’t know how to use the command line, check out our Bash courses.)
A Command Line Interface (Bash/zsh/Anaconda Prompt) might also open and display a url.
- If it does, do not close it until you are done working with Jupyter Notebook.
- If it does not appear, don’t worry — this has to do with your operating system and will not affect Jupyter Notebook’s performance.
When the notebook starts, you will be redirected to your default browser, and you will see a list of all the folders your program will have access to.
You can open a new Python file by selecting New >> Python3
The new Notebook will open in a new tab. Click on the title to rename it.
This is where you will write your code. You can write small or large chunks of code in each cell and run each cell independently of others.
To run a cell, you can use the Run button or Shift + Enter.
A new cell will appear after you run the cell you are working in. You can also add a new cell by clicking the + button at the top.
Writing Code in Jupyter Notebook
In this section, we will go through some examples of how to write code in Jupyter Notebook. This section is a preview of how notebooks work. For more on Jupyter’s useful features, check out the documentation.
When you write code in Jupyter Notebook, you will need to run each cell individually. Start with a basic print statement:
Notice that the result is printed below the cell.
Let’s try another one. This time, we will assign variables
x = 2 and
y = 3. When we run this cell, nothing appears in the output. Why?
Assigning variables doesn’t generate any output, so there is no result to write. We know the cell has finished running because a number appeared in the
[ ] (this is a temporary cell number). Let’s try using our
y variables. We can add them together.
x + y in the next cell.
Great! We got our expected value: 5.
Notice that we didn’t use a
print() statement, but Jupyter Notebook wrote the result anyhow.
Jupyter Notebook will only write the result of the last line but will print anything contained in a print statement no matter where it occurs. This can be confusing at first, so let’s look at an example. In the next cell, enter:
x + yx - y
This time only
-1 was returned. This is because Jupyter Notebook only displays the last line. If you want to display both, you have to wrap the first one in a
print(x + y)x - y
Try printing both lines on your own.
Jupyter Notebook basic features
Let’s work with some more complicated code. The purpose of this section is to try out some basic Jupyter Notebook features. If we use a function that you have not learned about yet, don’t worry, this section is only an example.
Let’s work with some data. We have a list of numbers:
mynums = [1, 2, 3, 4, 5]
Input this list into a cell and return the output.
mynums = [1, 2, 3, 4, 5]mynums
Now let’s try printing each item to the console.
for n in mynums:print(n+1)
Congratulations!! You are ready to start working with data in Jupyter Notebook.
The remainder of this article will introduce debugging and accessing documentation. You don’t need to know about these to start using Jupyter Notebook on your own. However, these features will support you on your learning journey (and beyond).
Jupyter Notebook has lots of built-in tools to help you learn to code. We will explore three of them here (the help documentation with
?, tab completion, and how to access the function arguments). This is just the beginning, check out this section of the Python Data Science Handbook for more tools.
Jupyter Notebook has a generalized help function with the
? character. Let’s say you want to know what object type
mynums is. You can use the
? to get full documentation about it.
Here you can see the object type, a preview of the data, and general information. You can do the same for functions such as
sum. Go ahead and try it in your own Notebook.
Jupyter Notebook also offers tab completion. Tab completion means that you can start typing, hit the Tab key, and Jupyter Notebook will offer suggestions about what could come next. For example, to turn our list into a pandas DataFrame, we can use the
.DataFrame() function. But you might forget how it is spelled (i.e., is it ‘f’ or ‘F’?). The tab function to the rescue! Let’s try it out.
First, import pandas.
import pandas as pd
D and hit the Tab button to see your options.
You can keep typing or select
DataFrame from the list. Now let’s finish our function:
That’s great, but sometimes you might know the function’s name but forget what arguments it needs.
Fortunately, Jupyter Notebook has a solution for that too. For example, you want to sort your list from largest to smallest. You know you need the
.sort() function, but you forgot what arguments you need to call. Place your cursor between the
( ) and hit the Shift + Tab keys.
That’s right! Specify reverse = True. Go ahead and try it out. Remember to call your list at the end of the cell so you can see the output.
Great job! As you code more, some functions will become second nature, but it can be helpful to know how to find the documentation until then.
Let’s move on to one more supportive feature in Jupyter Notebook: debugging.
Debugging with Jupyter Notebook
Now let’s try to create an error. There is no package named ‘panda’, but it is an easy typo to make. Let’s make that typo to see how Jupyter Notebook handles errors.
import panda as pd
As expected, we got an error. Jupyter Notebook tries to be as helpful as possible. There is a lot of information in this message, let’s break it down starting at the bottom. Our bottom line says:
ModuleNotFoundError: No module named 'panda'
Great! This error was easy to understand - “panda” doesn’t exist. If we look above our error message, we see that Jupyter Notebook also shows where this error occurred.
----> 1 import panda as pd
Sometimes the errors are more complicated, and you may not understand what they are saying. In that case, it can be helpful to copy the last line of the error and go to the internet for help (and check out this article on Troubleshooting).
Let’s try another example with a “magic command.” Magic commands are a group of special commands that help with a variety of non-coding tasks. For a complete list of the IPython magic commands, check out the documentation.
First, create a more complicated error:
x = 1+1y = 1-1z = x/y
You should get a
We want to debug this. In the next cell, type
%debug. A dialog box will appear that will let you inspect your code without creating new cells. Anything you write here will not be saved in your notebook.
A reminder of what did not work appears below. You can start by checking on each of those variables to figure out which one is causing a zero division error. In the
ipdb> cell, inspect
x by typing
x = 2, so that isn’t the problem. Try
y on your own.
The response should be
0. Great! We found our bug. Now we need to close the debug cell and get back to our program. To get out of debug mode, type
exit() in the
Congratulations! You are now ready to write, run, and debug your own programs in Jupyter Notebook.
This is just the beginning of the functions that Jupyter Notebook has to offer. For many more helpful tools and features, check out the documentation, and if you want to be inspired to do more with Jupyter notebook, check out A gallery of interesting Jupyter Notebooks.
Have fun coding with Jupyter Notebook!