IPython¶
IPython is an enhanced Python shell that creates a comprehensive environment for interactive and exploratory computing. In this section we’ll learn the basic features of IPython and how it benefits interactive analysis.
Before going further, open a new terminal window and change to your main Python for Astronomers working directory. Then start IPython by typing “ipython –matplotlib” at the command prompt:
$ ipython --matplotlib
As we saw in the Introduction and Installation workshops, for interactive data
analysis IPython has a special --matplotlib
command line option which makes
interactive plotting work better from a terminal window by allowing a plot to
come up without blocking subsequent terminal input.
In all of the tutorial examples we will start the session by importing the core modules numpy and matplotlib as follows:
import numpy as np
import matplotlib.pyplot as plt
The abbreviations np
and plt
provide quick access to numpy and
matplotlib routines and are widely used in scientific code.
Reminder: What does import
do?
In python only basic functionality is provided in the language itself. Most of the commands we need are imported from other modules. The import statement makes the functions in a module available:
print(time.ctime()) # will fail
# need to import the time module first
import time
time.ctime() # prints the system time
The as
variant of import
simply saves some typing. import numpy as np
allows us to type np
instead of numpy
to call a numpy function:
np.sum([2,3])
IPython with the --matplotlib
command line option provides a Matlab-like environment
allowing very simple and direct commands like the following:
x = np.arange(0, 10, 0.2)
y = np.sin(x)
print(x)
plt.plot(x, y)
Linux and shell commands¶
A select set of useful linux commands are available from the IPython prompt.
These include ls
(list directory), pwd
(print working directory),
cd
(change directory), and rm
(remove file). Any shell command
can be executed by preceding it with an exclamation point ”!”.
Tab completion¶
IPython has a very useful tab completion feature that can be used both to complete file names and to inspect python objects. As an example do:
ls ~/<TAB>
This will list everything in your home directory. You can continue this way searching through files or hit Return to complete the command.
Showing data values¶
So far we typed print x
to look at the value of x
. However,
most of the time for interactive analysis it is faster and better to simply
type x
(or whatever the object name) followed by <Return>. This returns
the “representation” of the object, which is often a cleaner and more
informative than the “string” version that gets returned with print
. In
many cases the “representation” of an object is the same as the Python
code used to create that object.
Try:
y = dict((x, 'value is %d' % x**2) for x in range(10))
y
print(y)
Further resources¶
- IPython docs page
- IPython customization :
E.g. to always import certain modules read The ipythonrc approach
which explains editing
~/.ipython/ipythonrc
and setting theimport_mod
configuration.