Jupyter tutorial

Follow this tutorial in order. Some parts depend on previous parts being run.



Part I: Cell Types

There are three types of cells: code, markdown, and raw.



In [ ]:
print "This is a code cell. It has the 'In []' to the left. You can run code in it."

This is a markdown cell.

You can think of it like a text cell.

You can use special characters (aka markdown) to create different text effects / sizes

Try the following:

  1. Double click on this cell
  2. See how you can make different effects
  3. Try it yourself
    • it's pretty swell!

This is a raw cell. It just has plain text. (zzz)

You can switch between cell types by going to Cell > Cell Type in the menu above. Or you can use the following hotkeys:

  • m - switch to markdown
  • y - switch to code
  • r - switch to raw


Part II: The Kernel (this is important)

When you launch a notebook, a Python "kernel" will automatically be started for that notebook. You can think of this as just an instance of Python that's waiting for you to give it code to run.

When code is running, you'll see the circle at the top right of the notebook will be filled in gray. If you hover over it, it'll say "Kernel busy". The kernel can only do one thing at a time, so you have to wait for it to finish before you can run another cell. However, you can still edit cells while you wait.

Sometimes you may want to stop the kernel while it's busy. This is called "interrupting" the kernel. To do this, hit the button with the square symbol up in the toolbar.

The code below creates what's called an "endless loop". It will keep running forever until you force it to stop. Try running it and use the kernel interrupt button to stop it.


In [ ]:
while True:
    pass

All the variables you create in a given notebook are saved by the kernel until you either (1) restart the kernel, or (2) close the notebook.

This means that when you create a variable in one cell, you can use that variable anywhere else in your notebook. Try this with the two cells below:


In [ ]:
cat = "Mittensworth"

In [ ]:
print cat

Sometimes you might want to clear all the stuff that's being stored by the kernel and start fresh. To do this, go to Kernel > Restart. You can also choose to Restart and clear all output. This will additionall clear the output underneath each cell. However, both clear the stored variables, so it doesn't matter which you pick. (The latter will probably lead to less confusion, though.)

Try restarting the kernel. Then run the code below:


In [ ]:
print cat

It should give you an error, because "cat" is no longer a variable known to the kernel. You would need to re-run the cell that defines the cat variable first for this to work.

Note, restarting is different than interrupting. Interrupting doesn't cause you to lose your variables; restarting does.



Part III: Other stuff with cells

You can do a lot of things with cells:

  • create them
  • delete them
  • move them
  • copy and paste them
  • merge and split them

Explore the menus at the top and try it out.

Sometimes you will write code that produces a lot of output. You can collapse the output by clicking (or double clicking) to the left of the output. Try it with the code below:


In [ ]:
for i in range(100):
    print i


Part IV: Hotkeys

Using Jupyter is a lot easier once you learn the hotkeys. Here are my favorites:

  • h - view all hotkeys
  • a - insert cell above selected cell
  • b - insert cell below selected cell
  • d,d (hit d twice) - delete selected cell
  • m / y - switch between markdown/code cells

You can only use these hotkeys when you're not "inside" a cell. Just hit Esc or click somewhere outside the cells to get out.



Part V: Autocompletion

One of my favorite features of Jupyter is autocompletion!

If you start typing the name of a variable that you've already created and then hit tab, it will fill in the rest of the variable name.

Try it below. Put your cursor right after the incomplete variable name in the print statement ("te") and hit tab.


In [ ]:
temperature = 37

print te

If there is more than one variable name that matches, it will show you a list. Use your arrow keys to select the one you want. Try it below:


In [ ]:
numBananas = 20
numApples = 12
numGrapes = 256

print nu

Autocompletion also works for file names on your computer! If you start typing a string that looks like a file name, Jupyter will try to autocomplete it for you. More on this later when we start doing file reading.

!!!Warning!!! autocompletion of ( ), [ ], {}, " ", ' '

On the flip side, one of the most annoying features of Jupyter (in my opinion...) is that it autocompletes braces and quotes. Usually it's not an issue, but in certain circumstances it can be a big pain and cause your code to have unexpected bugs.

The problem is that when you type a closing bracket (or quote) in front of an existing bracket (or quote), it does NOT insert a new bracket (quote). Instead, it jumps over the existing bracket.

In the example below, try typing a ) immediately after the 3:


In [ ]:
print (1 + (2 - 3) * 4

If you realize that this has happened, then it's not a big deal to correct (just type the closing bracket after). The problem is when you don't notice!