Previously we saw that the IPython shell allows you to access previous commands with the up and down arrow keys, or equivalently the Ctrl-p/Ctrl-n shortcuts. Additionally, in both the shell and the notebook, IPython exposes several ways to obtain the output of previous commands, as well as string versions of the commands themselves. We'll explore those here.
By now I imagine you're quite familiar with the
Out: style prompts used by IPython.
But it turns out that these are not just pretty decoration: they give a clue as to how you can access previous inputs and outputs in your current session.
Imagine you start a session that looks like this:
We've imported the built-in
math package, then computed the sine and the cosine of the number 2.
These inputs and outputs are displayed in the shell with
Out labels, but there's more–IPython actually creates some Python variables called
Out that are automatically updated to reflect this history:
In object is a list, which keeps track of the commands in order (the first item in the list is a place-holder so that
In can refer to the first command):
Out object is not a list but a dictionary mapping input numbers to their outputs (if any):
Note that not all operations have outputs: for example,
import statements and
None; for brevity, any command that returns
None is not added to
Where this can be useful is if you want to interact with past results.
For example, let's check the sum of
sin(2) ** 2 and
cos(2) ** 2 using the previously-computed results:
The result is
1.0 as we'd expect from the well-known trigonometric identity.
In this case, using these previous results probably is not necessary, but it can become very handy if you execute a very expensive computation and want to reuse the result!
The standard Python shell contains just one simple shortcut for accessing previous output; the variable
_ (i.e., a single underscore) is kept updated with the previous output; this works in IPython as well:
But IPython takes this a bit further—you can use a double underscore to access the second-to-last output, and a triple underscore to access the third-to-last output (skipping any commands with no output):
IPython stops there: more than three underscores starts to get a bit hard to count, and at that point it's easier to refer to the output by line number.
There is one more shortcut we should mention, however–a shorthand for
_X (i.e., a single underscore followed by the line number):
Sometimes you might wish to suppress the output of a statement (this is perhaps most common with the plotting commands that we'll explore in Introduction to Matplotlib). Or maybe the command you're executing produces a result that you'd prefer not like to store in your output history, perhaps so that it can be deallocated when other references are removed. The easiest way to suppress the output of a command is to add a semicolon to the end of the line:
Note that the result is computed silently, and the output is neither displayed on the screen or stored in the
For accessing a batch of previous inputs at once, the
%history magic command is very helpful.
Here is how you can print the first four inputs:
As usual, you can type
%history? for more information and a description of options available.
Other similar magic commands are
%rerun (which will re-execute some portion of the command history) and
%save (which saves some set of the command history to a file).
For more information, I suggest exploring these using the
? help functionality discussed in Help and Documentation in IPython.