Quick Introduction to Ipython Notebook

Purpose of this notebook: Introduce you to:

  1. the basic features of interactive python that you use from a web browser
  2. numpy (http://www.numpy.org), which you need to do scientific analysis in python
  3. matplotlib (http://www.matplotlib.org), which you need to make nice, 2D plots in python
  4. ways to get help while learning

At the end of this lesson, you should be able to display a plot of $y = x^2$.

This notebook has:

a title (that can be renamed by clicking on it),

a main menu bar with functions for adding content,

and a toolbar with often-used tools. Try the Help->User Interface Tour to see each of these parts pointed out to you.

Here is more stuff.

Here is some raw text.

Notebooks consists of "cells" that can contain markdown, comments, executable code snippets and more. "Execute" a cell by entering Shift-Return

or clicking Cell->Run on the menu

or clicking the play button on the toolbar.

In a cell, you can move the cursor around with:

  • Ctrl-a : beginning of line
  • Ctrl-e : end of line
  • Ctrl-k : cut to end of line
  • Ctrl-p : go to previous line
  • Ctrl-n : go to next line

In [ ]:
print( "Hello world\n")

Python has a notion of "namespaces," which requires you to "import" modules that are not available as a default. One of the most important of these for scientific computing is numpy (Numerical Python http://www.numpy.org/). While python has some basic types for floats, ints, lists and dicts (structures), if you are doing anything with vectors or matrices of numbers, you should be using numpy. Here's an example:


In [ ]:
import numpy as np
x = np.array(  [ [ 1, 2 ], [ 3, 4 ] ] )
x

In [ ]:
print x

A handy feature while learning about something new in python is to call the help function on it (you can do this on objects, variables, or functions to get more information about them).


In [ ]:
help( x )

Another very valuable python module that you will likely want to learn is called matplotlib (http://matplotlib.org/). It is a python module intended for users of matlab. So if you are a matlab user, you will be very comfortable with it! (I am not a matlab user, I know IDL a lot better, so I struggle with it a little bit.) But first, here's a numpy function for generating a sequence of N numbers from zero to N-1:


In [ ]:
help( np.arange )

In [ ]:
# You only need to execute this command once, 
# at the beginning of your notebook session
%pylab inline  
import matplotlib.pyplot as plt
plt.plot(np.arange(150))
#plt.show()  # this line may not be needed (depends on matplotlib.is_interactive() setting

(Use the little icon in the lower right part of the plot to resize it interactively.)


In [ ]:
data = np.arange( 150 )
fig, ax = plt.subplots(1,1)
ax.set_title('My title')
plt.plot( data )

In an ipython notebook like the one we are using here, there are a lot of options for displaying text, notes and images. Just one more example (because it's so cool), is that you can display latex-formatted equations very easily if you are using math equations. Like this:

$$k_{0} = \cos{\phi_{1}} { \sqrt{ 1 - e^2\sin^2{\phi_{1}}}}$$

Further examples and tutorial on ipython notebooks:
http://ipython.org/ipython-doc/1/interactive/notebook.html

Try using the command cell below this to display a plot of $y = x^2$. Give it a title like this: "<your name>'s first python plot".

Hint: One way python has for raising a value to a power is an operator that looks like 2 asterisks: "**".


In [ ]: