Jupyter Notebooks

  • Markdown

  • code cells

  • kernel

Jupyter notebooks are a way to combine executable code, code outputs, and text into one connected file.
The official documentation from project Jupyter is available here and they also have some example notebooks here .

Cells

The main organizational structure of the notebook are 'cells'.

Cells are an independent ‘unit’. When you click into a cell, you can ‘run’ it by clicking Shift + Enter, or by pressing the play (Run) button above.

Cells come in different types for writing different things - mainly, text or code.

Markdown Cells

Cells, can be markdown (text), like this one.

A brief note about Markdown. It’s a way to specify all formatting within the text itself.

For example, italicized text can be specified with an underscore or single asterisks.

Bold text requires two underccores or two asterisks.

Clicker Question #2

What does three underscores around text accomplish?

  • A) bold

  • B) italicize

  • C) bold + italicize

  • D) normal text

  • E) I’m lost

you can write your markdown text here to determine the answer to the question

Markdown Headers

Headers are specified with a pound sign

The more pound signs, the smaller the header

But it’s still larger

than just plain text.

Lists are also possible:

  • item 1

  • item 2

  • item 3

  1. numbered item

  2. item 2

  3. item 3

Clicker Question #3

What would happen if I specified a numbered list but put the same number before each list item?

  • A) the list would have the same number before each item

  • B) markdown would still format it with sequential numbers

  • C) markdown wouldn’t know it was a list

  • D) normal text with everything on a single line

  • E) I’m lost

test it out down here to see…

  1. list item

  2. list item

  3. list item

* <- escape character

Code Cells

Whenever you’re writing code, you’ll want to be sure the cell is set to be a code cell

# Cell can also be code.
a = 1
b = 2
# Cells can also have output, that gets printed out below the cell.
c = a - b
print(c)
-1
# If you execute a cell with just a variable name in it, it will also get printed
c
-1

Running Cells

  • The numbers in the square brackets to the left of a cell show which cells have been run, and in what order.

    • An asterisk (*) means that the cell is currently running

    • You do not need to run cells in order! This is useful for flexibly testing and developing code.

Coding time

Write code that outputs the value ‘6’

## YOUR CODE HERE
a = 12
b = 1/2
print(b)
c = a * b

c
0.5
6.0

Clicker Question #5

Which of the following best describes you?

  • A) I completed the task.

  • B) I tried but wasn’t able to complete the task.

  • C) I am not sure where to start.

Accessing Documentation

Jupyter has useful shortcuts. Add a single '?' after a function or class get a window with the documentation, or a double '??' to pull up the source code.
# For example, execute this cell to see the documentation for the 'abs'
abs?

Autocomplete

Jupyter also has tab complete capacities, which can autocomplete what you are typing, and/or be used to explore what code is available. (Note: Some of this functionality is limited on datahub.)
# Move your cursor to the end of the line, press tab, and a drop menu will appear showing all possible completions
ra
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-c2c498cb5d74> in <module>()
      1 # Move your cursor to the end of the line, press tab, and a drop menu will appear showing all possible completions
----> 2 ra

NameError: name 'ra' is not defined
# If there is only one option, tab-complete will auto-complete what you are typing
ran

Web Browser

Jupyter notebooks display in a web browser. They are not hosted on the web, everything is happening locally.

If you click on the url in the browser, you will notice it says ‘localhost’. This means it is connected to something locally, on your computer.

That local connection is to the ‘kernel’.

Kernels

The kernel is the thing that executes your code. It is what connects the notebook (as you see it) with the part of your computer that runs code.

Your kernel also stores your namespace - all the variables and code that you have declared (executed).

It can be useful to clear and re-launch the kernel. You can do this from the ‘kernel’ drop down menu, at the top, optionally also clearing all ouputs. Note that this will erase any variables that are stored in memory.

For more useful information, check out Jupyter Notebooks tips & tricks , and more information on how notebooks work.