2-0 IPython and the IPython Notebook

Alright now that I’ve introduced the course, I wanted to introduce you the tools that we'll be using.

Throughout this course I'm going to be using an IPython notebook. The IPython notebook is a part of the anaconda distribution that I recommended before.

IPython is an interactive computing interface for python that adds some extremely helpful tools to python. Let’s go ahead and get started with that.

We start the IPython Notebook from the command like by just typing in ipython notebook. It has a lower level interface, IPython, but I won't be covering that in this course.

The IPython Notebook adds lots of helper commands, adds tab completion, general developer happiness tools to python.

An IPython notebook is made up of cells. There are various kinds of cells that I can set up from this top menu. Simply there are code cells that can be executed and text cells that can be styled.

Let's start with a simple command in this first cell.


In [1]:
from __future__ import print_function # python 3 support from python 2

In [2]:
print("Hello IPython")


Hello IPython

You'll see that we can see the output of any command that we execute in thse cells. Now cells all share the same global environment. So if we set a variable in one, we can print it out in another.


In [3]:
x = 5

In [4]:
print(x)


5

We can also define functions in cells and access those in others. (use tab completion)


In [5]:
def square(input_var):
    return input_var * input_var

you also saw right there that I can use tab completion to complete those variables. That's another handy thing about the IPython notebook. Tab completion.

We can also get access to certain shell commands like "ls" to list everything in the current directory.


In [6]:
square(x)


Out[6]:
25

In [7]:
ls


2-0 The IPython Notebook.ipynb              2-1 Raw Python - Maps.ipynb                 2-2 Raw Python - Filters.ipynb              2-3 Raw Python - List Comprehensions.ipynb  2-4 Raw Python - Lambda Functions.ipynb

We also get access to some "magic commands". These commands are baked into IPython and provide us some really, really helpful ways of writing code without having to google things all the time.

For example, quickref


In [8]:
%quickref

Quickref brings up the quickreference for IPython, we can see all the commands that are made available to us.

Some of my favorites are the ? and %timeit

Here's a demonstration of the ? helper. Say I've imported a module, let's say datetime and I forgot the order of arguments when creating a new one.


In [9]:
from datetime import datetime

In [10]:
?datetime

All I have to do is put a question mark either before or after it and it will bring up the documentation for that specific module or function.


In [11]:
datetime?

In [12]:
?square

This is super handy and something you'll be seeing through out this course because I use it a lot when I'm working in IPython notebooks. Timeit is also useful because it allows for easy profiling.


In [13]:
%timeit square(x)


The slowest run took 14.49 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 168 ns per loop

Now I will warn you, I’ve used IPython Notebooks and while they are great for rapid prototyping and sharing your scientific code or analysis they're not so great for writing production systems.

However the notebook interface allows you to save a .py file so that you may build on your basic prototype into a production system. There's a lot of functionality built in to the IPython notebook and you could make a whole course on it. Please feel free to ask questions on the message boards and I'll be sure to answer them as quickly as I can.

Now that I’ve introduced the basics of this tool we can start diving into the material.