Let's Go!

This is a Jupyter notebook --- my first, in fact. Jupyter is an interface to many common scientific computing tools. The ones that give it its name are Julia, Python, and R. (Apparently the e was added because "Jupytr" looks strange.) Julia is a technical computing language, similar in some respects to MATLAB. Python is the computing language that we know and love. R is a statistics and graphics package widely used in data analysis. The makers of Jupyter seem to understand that no single solution exists for all scientific computing needs. So they have created a single interface for many useful open source software packages.

The primary benefit of Jupyter over command line coding or an IDE like Spyder is that you can integrate text, mathematics, code, and graphics in a single document that can be shared in a variety of formats.

Cells

A notebook is divided into cells. These may contain formatted text, code, or raw input. To execute the contents of a cell, type <Ctrl-Enter>, or select one of the many Run options from the Cell menu at the top of the page.

Take a quick look at all of the menu options and Help > Keyboard Shortcuts to get an idea of how to interact with a Jupyter notebook.

Text

Text is rendered using the MarkDown language. This allows you to type in plain text and use a few formatting commands to embellish the output when it is rendered. You can also enter raw HTML code if you are so inclined.

For example, you can emphasize text:

  • Typing "*italics*" or "_italics_" yields italics.
  • Typing "**boldface**" or "__boldface__" yields boldface.
  • Typing "***bold italics***" or "___bold italics___" yields bold italics.

You can also create headings, subheadings, and horizontal rules with ease.

-----
# Title
## Section
### Subsection
#### Subsubsection
-----

produces the following output:


Title

Section

Subsection

Subsubsection


You can do a lot more with MarkDown: insert images, create Web links, cross reference, etc. Just read the manual or this cheatsheet!

Math

You can enter math directly into the notebook. Jupyter uses MathJax, a Java script that implements LaTeX commands. You will have to spend a little time learning the LaTeX syntax, but once you do, you can have beautiful equations alongside your explanatory text, code, and graphics:

The golden ratio $\phi$ is one solution to $x^2 = x + 1$.

$$ e^{i\pi} + 1 = 0 $$$$ \int_{-\infty}^{\infty} dx \ e^{-x^2} = \sqrt{\pi} $$

Here is a brief introduction to LaTeX commands in MathJax.

Code

It is extremely simple to write and run Python code. Create a new cell, and type the Python code just as you would in a script or in another interpreter. When you are finished, you can execute the code in the cell by typing <Shift+Enter>:

In [1]:
print("Hello, world!")
Hello, world!

Graphics

If your code generates graphics, you can display them in the notebook as well. First, you need to specify that graphics will be "inline". Otherwise, they will be handled by another graphics backend, outside of your notebook.

In [5]:
import numpy as np
import matplotlib.pyplot as plt

# This will force graphics to be displayed in the notebook.
%matplotlib inline

Now, you can create plots that will show up right below the code:

In [6]:
x = np.linspace(-2,2,101)
y = (1-x**2)*np.exp(-x**2)

plt.plot(x,y,'b-', lw=3)
plt.xlim(-2,2)
plt.ylim(-0.5,1.25)
Out[6]:
(-0.5, 1.25)

If you are using IPython 3.0 or later, you can even create interactive graphics in your notebook. This allows you to zoom, re-center, resize, and save the figure. You can also rotate the axes of 3D figures.

In [7]:
# This will create interactive plots.
%matplotlib notebook
In [9]:
from mpl_toolkits.mplot3d import Axes3D     # Import 3D plotting tools.
from scipy.special import jn                # Import Bessel function.

# Define grid of points.
points = np.linspace(-10, 10, 51)
X, Y = np.meshgrid(points, points)
R = np.sqrt(X**2 + Y**2)
Z = jn(0,R)

# Create 3D surface plot.
ax = Axes3D(plt.figure())
ax.plot_surface(X, Y, Z, rstride=1, cstride=1)
plt.show()

Get Going!

You will learn a lot more by playing around with notebooks yourself than you will by reading mine and excuting my code.

If you have recently installed or updated the Anaconda distribution of Python, you already have access to Jupyter notebooks. You can launch one from the Launcher.app program that came with Anaconda. (IPython notebooks are now Jupyter notebooks, but you may still see "IPython Notebook" in the Launcher.)

If you did not do a full installation of Anaconda, you can install Jupyter from the command line.

>> conda update conda
>> conda install jupyter

Afterwards, you can create a new Jupyter notebook with the following command:

>> jupyter notebook

This will open a window in your Web browser that displays the files and folders in the current directory. Open a new notebook by selecting New > Python 3 from the New menu at the upper right of the list of files.

You can download this notebook. Open it by double-clicking on the saved file, by using File > Open... from the menu in an open notebook, or type

>> jupyter notebook Introduction.ipynb

at the command line.

Open this notebook --- or, better yet, create your own --- and start exploring!