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.
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.
The following steps will get you up and running with the IPython environment:
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]:
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.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.?
, and you will get a quick introduction to and overview of IPython's features.%
symbol. The %paste
function is one such example and is amazing for copying and pasting Python code into IPython without losing proper indentation.%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.
!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
%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
exit
to drop out of IPython and back to your system command prompt.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.
13.59.177.14