IPython reference


Loosely Based on Lecture Materials By: Milad Fatenejad, Katy Huff, Joshua R. Smith, Tommy Guy, Will Trimble, and Many More

Getting Started

You run code using Shift-Enter. This also moves you to the next box (or "cell") for code below the one you just ran (but this may change in the future).

You can run the following code using Shift-Enter now!


In [ ]:
print "Look Ma, I'm programming!"

The box above is called the input cell; commands you put here will be fed to the python interpreter one at a time when you press Shift-ENTER.

The output of the command, or the error message, appears below the line you entered it on.

The panel which may appear on the left has some notebook options; you can minimize the panel by double-clicking on the bar separating the windows.


In [ ]:
print "Try and tell that to the young people"

print "of today--they won't believe you."

If you hit ENTER only, ipython notebook gives you another line in the current cell.

This allows you to compose multi-line commands and submit them to python all at once.

Up and down arrows will allow you to move the cursor to different cells in the notebook, including these cells containing text (which you can edit in your browser).

Only the cells for which you press Shift-ENTER or Control-ENTER will be executed by the python interpreter.

You can enter the same line over and over again into the interpreter. It's wierd, but it's life.


In [ ]:
i = 0

Shift-ENTER executes and moves to the next cell.

Control-ENTER executes and does not move to the next cell.

(But again, this may change.)

Try entering this cell a few times:


In [ ]:
i = i + 1

print i

If you want to create new empty cells, it's three keys: Shift-Enter, Control-M, and then a This will insert more cells in the middle of the notebook.

Getting Help

IPython has some nice help features. Let's say we want to know more about the integer data type. There are at least two ways to do this task:


In [ ]:
help(int)

which displays a scrolling text help, or


In [ ]:
int?

Which displays a shorter help summary in the magic pane at the bottom of the screen. You can minimize the magic pane when it gets in your way.

If you wanted to see all the built-in commands available for something, use the dir command. Check out all of the methods of the object "Hello world", which are shared by all objects of the str type.


In [ ]:
dir("Hello world")

There's a method that looks important -- swapcase. Let's see what it does:


In [ ]:
"Hello world".swapcase()

Hrm. Ahem.

Executing code in files

If your code is in a file, you can execute it from the IPython shell with the %run command. Execute hello.py like so


In [ ]:
%run hellp.py

Ooops. We misspelled hello.py, and python is giving us an error message. Change the line above to hello.py, hit Shift-ENTER, and see what it does.

Clearing IPython

To clear everything from IPython, use the %reset command or Kernel->Restart in the menu.


In [ ]:
mystring = "And three shall be the count." 

print mystring

In [ ]:
%reset # or Kernel->Restart in the menu

In [ ]:
print mystring

Pasting

You can paste things into the ipython console by copying text from your machine with ctrl+c and typing %paste at the IPython prompt. The %paste is necessary syntax for multi-line clipboard deposits. A related command is %cpaste. But this is only relevant for the console, NOT the web notebook!