Getting Started with IPython and Python for scientific computing

First things first: how do I get some help?

In [1]:
?
In [2]:
%quickref

Basic concepts

In [3]:
%magic
In [4]:
%alias?
In [5]:
!pwd
/home/fperez/teach/reprosw

Tab completion!

In [10]:
import numpy as np
np
Out[10]:
<module 'numpy' from '/home/fperez/usr/opt/lib/python2.7/site-packages/numpy/__init__.pyc'>

Using IPython as a calculator

In [13]:
2+3
Out[13]:
5
In [14]:
_ **2
Out[14]:
25
In [17]:
2**100
Out[17]:
1267650600228229401496703205376L

This is Python, after all!

In [18]:
def factorial(n):
    if n==0:
        return 1
    return n*factorial(n-1)
In [19]:
factorial(10)
Out[19]:
3628800
In [20]:
import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Plotting in the notebook

This imports numpy as np and matplotlib's plotting routines as plt, plus setting lots of other stuff for you to work interactivel very easily:

In [21]:
%pylab inline
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
In [23]:
x = np.linspace(0, np.pi, 200)
y = np.sin(x**2)
plt.plot(x, y)
Out[23]:
[<matplotlib.lines.Line2D at 0x43f8890>]
In [ ]: