Getting started with IPython

IPython is the interactive computing shell for Python that will change the way you think about interactive shells. It brings to the table a host of very useful functionalities that will most likely become part of your default toolbox, including magic functions, tab completion, easy access to command-line tools, and much more. We will only scratch the surface here and strongly recommend that you keep exploring what can be done with IPython.

Getting ready

If you have completed the installation instructions in the first chapter, you should be ready to tackle the following recipes. Note that IPython 2.0, which is a major release, was launched in 2014.

How to do it…

The following steps will get you up and running with the IPython environment:

  1. Open up a terminal window on your computer and type ipython. You should be immediately presented with the following text:
    Python 2.7.5 (default, Mar  9 2014, 22:15:05)
    Type "copyright", "credits" or "license" for more information.
    
    IPython 2.1.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    In [1]: 
    

    Tip

    Note that your version might be slightly different than what is shown in the preceding command-line output.

  2. Just to show you how great IPython is, type in ls, and you should be greeted with the directory listing! Yes, you have access to common Unix commands straight from your Python prompt inside the Python interpreter.
  3. Now, let's try changing directories. Type cd at the prompt, hit space, and now hit Tab. You should be presented with a list of directories available from within the current directory. Start typing the first few letters of the target directory, and then, hit Tab again. If there is only one option that matches, hitting the Tab key automatically will insert that name. Otherwise, the list of possibilities will show only those names that match the letters that you have already typed. Each letter that is entered acts as a filter when you press Tab.
  4. Now, type ?, and you will get a quick introduction to and overview of IPython's features.
  5. Let's take a look at the magic functions. These are special functions that IPython understands and will always start with the % symbol. The %paste function is one such example and is amazing for copying and pasting Python code into IPython without losing proper indentation.
  6. We will try the %timeit magic function that intelligently benchmarks Python code. Enter the following commands:
    n = 100000
    %timeit range(n)
    %timeit xrange(n)
    

    We should get an output like this:

    1000 loops, best of 3: 1.22 ms per loop
    1000000 loops, best of 3: 258 ns per loop
    

    This shows you how much faster xrange is than range (1.22 milliseconds versus 2.58 nanoseconds!) and helps show you the utility of generators in Python.

  7. You can also easily run system commands by prefacing the command with an exclamation mark. Try the following command:
    !ping www.google.com
    

    You should see the following output:

    PING google.com (74.125.22.101): 56 data bytes
    64 bytes from 74.125.22.101: icmp_seq=0 ttl=38 time=40.733 ms
    64 bytes from 74.125.22.101: icmp_seq=1 ttl=38 time=40.183 ms
    64 bytes from 74.125.22.101: icmp_seq=2 ttl=38 time=37.635 ms
    
  8. Finally, IPython provides an excellent command history. Simply press the up arrow key to access the previously entered command. Continue to press the up arrow key to walk backwards through the command list of your session and the down arrow key to come forward. Also, the magic %history command allows you to jump to a particular command number in the session. Type the following command to see the first command that you entered:
    %history 1
    
  9. Now, type exit to drop out of IPython and back to your system command prompt.

How it works…

There isn't much to explain here and we have just scratched the surface of what IPython can do. Hopefully, we have gotten you interested in diving deeper, especially with the wealth of new features offered by IPython 2.0, including dynamic and user-controllable data visualizations.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.177.14