Hello Python and Jupyter

In this notebook you will:

  • Learn how to use a Jupyter notebook such as this one
  • Get a very quick tour of Python syntax and scientific libraries
  • Learn some IPython features that we will use in later tutorials

What is this?

This is Jupyter notebook running in a personal "container" just for you, stocked with example data, demos, and tutorials that you can run and modify. All of the software you'll need is already installed and ready to use.

Run some Python code!

To run the code below:

  1. Click on the cell to select it.
  2. Press SHIFT+ENTER on your keyboard or press the button in the toolbar above (in the toolbar above!).
In [ ]:
1 + 1

Notice that you can edit a cell and re-run it.

The notebook document mixes executable code and narrative content. It supports text, links, embedded videos, and even typeset math: $\int_{-\infty}^\infty x\, d x = \frac{x^2}{2}$

Whirlwind Tour of Python Syntax

Lists

In [ ]:
stuff = [4, 'a', 8.3, True]
In [ ]:
stuff[0]  # the first element
In [ ]:
stuff[-1]  # the last element

Dictionaries (Mappings)

In [ ]:
d = {'a': 1, 'b': 2}
In [ ]:
d
In [ ]:
d['b']
In [ ]:
d['c'] = 3
In [ ]:
d

Functions

In [ ]:
def f(a, b):
    return a + b

In IPython f? or ?f display information about f, such as its arguments.

In [ ]:
f?

If the function includes inline documentation (a "doc string") then ? displays that as well.

In [ ]:
def f(a, b):
    "Add a and b."
    return a + b
In [ ]:
f?

Arguments can have default values.

In [ ]:
def f(a, b, c=1):
    return (a + b) * c
In [ ]:
f(1, 2)
In [ ]:
f(1, 2, 3)

Any argument can be passed by keyword. This is slower to type but clearer to read later.

In [ ]:
f(a=1, b=2, c=3)

If using keywords, you don't have to remember the argument order.

In [ ]:
f(c=3, a=1, b=2)

Fast numerical computation using numpy

For numerical computing, a numpy array is more useful and performant than a plain list.

In [ ]:
import numpy as np

a = np.array([1, 2, 3, 4])
In [ ]:
a
In [ ]:
np.mean(a)
In [ ]:
np.sin(a)

We'll use the IPython %%timeit magic to measure the speed difference between built-in Python lists and numpy arrays.

In [ ]:
%%timeit

big = list(range(10000))  # setup line, not timed
sum(big)  # timed
In [ ]:
%%timeit

big = np.arange(10000)  # setup line, not timed
np.sum(big)  # timed

Plotting using matplotlib

In an interactive setting, this will show a canvas that we can pan and zoom. (Keep reading for what we can do in a non-interactive setting, such as the static web page version of this tutorial.)

In [ ]:
# We just have to do this line once, before we do any plotting.
%matplotlib widget
import matplotlib.pyplot as plt

plt.figure()

We can plot some data like so. In an interactive setting, this will update the canvas above.

In [ ]:
plt.plot([1, 1, 2, 3, 5, 8])

And we can show a noninteractive snapshot of the state of the figure at this point by display the figure itself.

In [ ]:
plt.gcf()

Displaying plt.gcf() (or any Figure) shows a non-interactive snapshot of a figure. Displaying plt.gcf().canvas or any Canvas gives us another interactive, live-updating view of the figure.

Interrupting the IPython Kernel

Run this cell, and then click the square 'stop' button in the notebook toolbar to interrupt the infinite loop.

(This is equivalent to Ctrl+C in a terminal.)

In [ ]:
# This runs forever -- hit the square 'stop' button in Jupyter to interrupt
# The following is "commented out". Un-comment the lines below to run them.
# while True:
#     continue

"Magics"

The code entered here is interpreted by IPython, which extends Python by adding some conveniences that help you make the most out of using Python interactively. It was originally created by a physicist, Fernando Perez.

"Magics" are special IPython syntax. They are not part of the Python language, and they should not be used in scripts or libraries; they are meant for interactive use.

The %run magic executes a Python script.

In [ ]:
%run scripts/hello_world.py

When the script completes, any variables defined in that script will be dumepd into our namespace. For example (as we will see below), this script happens to define a variable named message. Now that we have %run the script, message is in our namespace.

In [ ]:
message

This behavior can be confusing, in the sense that the reader has to do some digging to figure out where message was defined and what it is, but it has its uses. Throughout this tutorial, we will use the %run magic as a shorthand for running boilerplate configuration code and defining variables representing hardware.

The %load magic copies the contents of a file into a cell but does not run it.

In [ ]:
%load scripts/hello_world.py

Execute the cell a second time to actually run the code. Throughout this tutorial, we use the %load magic to load solutions to exercises.

System Shell Access

Any input line beginning with a ! character is passed verbatim (minus the !, of course) to the underlying operating system.

In [ ]:
!ls