In [1]:

    import numpy, scipy, matplotlib.pyplot as plt, pandas, librosa

← Back to Index

# Getting Good at IPython¶

You are looking at an IPython Notebook, an interactive Python shell inside of a web browser. With it, you can run individual Python commands and immediately view their output. It's basically the Matlab Desktop or Mathematica Notebook for Python.

First, we recommend that you take the User Interface Tour in the Help Menu above.

## Cells¶

An IPython Notebook is comprised of cells. Cells are just small units of code or text. For example, the text that you are reading is inside a Markdown cell. (More on that later.)

Code cells allow you to edit, execute, and analyze small portions of Python code at a time. Here is a code cell:

In [2]:

    1+2

Out[2]:

    3

## Modes¶

The IPython Notebook has two different keyboard input modes.

In Edit Mode, you type code/text into a cell. Edit Mode is indicated by a green cell border.

To enter Edit Mode from Command Mode, press `Enter`. You can also double-click on a cell.

To execute the code inside of a cell and move to the next cell, press `Shift-Enter`. (`Ctrl-Enter` will run the current cell without moving to the next cell. This is useful for rapidly tweaking the current cell.)

In Command Mode, you can perform notebook level actions such as navigating among cells, selecting cells, moving cells, saving notebooks, displaying help. Command Mode is indicated by a grey cell border.

To enter Command Mode from Edit Mode, press `Esc`. Other commands can also enter Command Mode, e.g. `Shift-Enter`.

To display the Help Menu from Command Mode, press `h`. Use it often; `h` is your best friend.

## Saving¶

Your code goes directly into an IPython notebook. To save your changes, click on the "Save" icon in the menu bar, or type `s` in command mode.

If this notebook is in a Git repo, use `git checkout -- <file>` to revert a saved edit.

## Writing Text in Markdown¶

Markdown is simply a fancy way of formatting plain text. It is a markup language that is a superset of HTML. The Markdown specification is found here: http://daringfireball.net/projects/markdown/basics/

A cell may contain Python code or Markdown code. To convert any Python cell to a markdown cell, press `m`.

For headings, we recommend that you use IPython's keyboard shortcuts. To change the text in a cell to a level-3 header, simply press `3`. For similar commands, press `h` to view the Help menu.

## Writing Text in $\LaTeX$¶

In a Markdown cell, you can also use $\LaTeX$ syntax. Example input:

    $$ \max_{||w||=1} \sum_{i=1}^{N} \big| \langle w, x_i - m \rangle \big|^2 $$

Output:

$$ \max_{||w||=1} \sum_{i=1}^{N} \big| \langle w, x_i - m \rangle \big|^2 $$

## Default Imports¶

If you run IPython inside the `stanford-mir` Vagrant box used for this workshop, every IPython notebook will automatically have the following packages imported:

    import numpy
    import scipy
    import pandas
    import matplotlib
    import matplotlib.pyplot as plt
    import librosa

Example:

In [3]:

    plt.plot(scipy.hamming(101))

Out[3]:

    [<matplotlib.lines.Line2D at 0x108759110>]

To learn more about automatic imports, view the IPython configuration file at `~/.ipython/profile_default/ipython_config.py`.

## Tab Autocompletion¶

Tab autocompletion works in Command Window and the Editor. After you type a few letters, press the `Tab` key and a popup will appear and show you all of the possible completions, including variable names and functions. This prevents you from mistyping the names of variables -- a big time saver!

For example, type `scipy.` and then press `Tab`. You should see a list of members in the Python package `scipy`.

Or type `scipy.sin`, then press `Tab` to view members that begin with `sin`.

In [4]:

    # Press Tab at the end of the following line
    scipy.sin

Out[4]:

    <ufunc 'sin'>

## Inline Documentation¶

To get help on a certain Python object, type `?` after the object name, and run the cell:

In [5]:

    # Run this cell.
    int?

In addition, if you press `Shift-Tab` in a code cell, a help dialog will also appear. For example, in the cell above, place your cursor after `int`, and press `Shift-Tab`. Press `Shift-Tab` twice to expand the help dialog.

## More Documentation: NumPy, SciPy, Matplotlib¶

In the top menu bar, click on Help, and you'll find a prepared set of documentation links for IPython, NumPy, SciPy, Matplotlib, and Pandas.

## Experimenting¶

Code cells are meant to be interactive. We may present you with several options for experimentation, e.g. choices of variables, audio files, and algorithms. For example, if you see a cell like this, then try all of the possible options by uncommenting the desired line(s) of code. (To run the cell, select "Cell" and "Run" from the top menu, or press `Shift-Enter`.)

In [6]:

    x = scipy.arange(50)
    # Try these too:
    # x = scipy.randn(50)
    # x = scipy.linspace(0, 1, 50, endpoint=False)
    x

Out[6]:

    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
           17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
           34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])

← Back to Index