Skip to content

Instantly share code, notes, and snippets.

@jamesp
Last active December 29, 2015 21:59
Show Gist options
  • Save jamesp/7733525 to your computer and use it in GitHub Desktop.
Save jamesp/7733525 to your computer and use it in GitHub Desktop.

iPython basics

Using the console

  • autcomplete works on variable and function names
  • Up arrow takes you back to previously entered commands
  • %quickref gives an overview of available functionality.

magic commands

iPython has "magic" commands which are an set of meta functions for performing basic actions in the console.

For example, %paste will take the contents of your clipboard and execute in the console.

For example, you can interact with the code you've written in this session and in previous iPython sessions:

%%history -n
%rerun 75
%save my.py 1-10
%logstart
%logon
%logoff

%lsmagic gives a list of all available commands.

Profiling

iPython has builtin support for the timeit library via magic functions:

%timeit fib1(10)

Using 2 %% signs instead of one %%timeit puts you into a "cell mode" where you can enter multiple lines of code and the profiler will be run over all of them after you enter a blank line.

The %timeit functions are smart and checks how fast one run of your code takes before committing to doing a million repeats. Slower functions are repeated less.

def fib1(n):
    if n==0 or n==1:
        return 1
    else:
        return fib1(n-1)+fib1(n-2)

def fib3(n):
    a,b = 0,1
    for _ in range(n):
        a, b = b, a+b
    return b

? and ?? introspection

  • adding a ? to the end of a function shows you the method signature and docstring: math.log?
  • ?? shows more detailed information. For native python functions it shows the full source code of the function. ```np.log??``
  • Numpy has detailed documentation for most functions - equivalent to that hosted on their website.
  • ? and ?? can be used on magic functions too.

shell commands

The exclamation mark followed by a shell command will execute the command in the underlying OS console. This output can also be passed directly into a python variable.

!ls ports = !netstat

There are better ways of doing this sort of thing if you are writing a robust set of functions, but for prototyping this can be useful for quickly accessing other output from python.

debugging

iPython has a powerful debugger built into the console. One of the most useful features of it is that you don't need to have it running before you encounter an error. If your code fails, the last error state is preserved. Using the magic command %debug you will enter the debugger at the point at which your code last failed, where you can check the value of your locals etc.

To use the debugger from the start of your fuction, or with a list of statements you can enter a debugging cell-mode using the %%debug magic function.

ipdb has all the normal commands you'd expect from a gdb inspired debugger, use help to list all available commands, and help <command> for more information on how to use it.

def crazy_loop(var):
    x = 0
    for i in range(var):
        x += 25 / (i - 3)*(i - 6)
    return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment