input_output_history
Input/Output History
Section titled “Input/Output History”IPython meticulously records the commands you enter and the results they produce during a session. This history is invaluable for reviewing your work, recalling previous commands, and reusing results.
Accessing Previous Commands
Section titled “Accessing Previous Commands”The simplest way to access previous commands is using the Up and Down arrow keys in the IPython console or a Jupyter cell. This scrolls through the history of executed commands in the current session.
IPython also provides more structured ways to access history:
The In Object and Related Variables
Section titled “The In Object and Related Variables”IPython stores all previous inputs (the code you typed) in a list-like object called In. You can access specific inputs using their cell number (the number shown in In[ ]). Note that In[0] is typically an empty string.
In [1]: message = "Hello IPython"
In [2]: x = 10
In [3]: y = 20
In [4]: x + yOut[4]: 30
In [5]: In[1]Out[5]: 'message = "Hello IPython"'
In [6]: In[1:4] # Slice notation worksOut[6]: ['message = "Hello IPython"', 'x = 10', 'y = 20']There are also shorthand variables for the last three inputs:
_i: Previous input._ii: Input before the previous one._iii: Input two commands before the previous one.
In [7]: _i # Input of cell [6]Out[7]: 'In[1:4] # Slice notation works'
In [8]: _ii # Input of cell [5]Out[8]: 'In[1]'
In [9]: _iii # Input of cell [4]Out[9]: 'x + y'Additionally, _ih (input history) is a list containing all input history as strings.
The %history Magic Command
Section titled “The %history Magic Command”The %history (or %hist) magic command provides a formatted view of the input history.
In [10]: %history# Output:# message = "Hello IPython"# x = 10# y = 20# x + y# In[1]# In[1:4] # Slice notation works# _i # Input of cell [6]# _ii # Input of cell [5]# _iii # Input of cell [4]# %history
In [11]: %history -n 4-6 # Show history with line numbers 4 to 6# Output:# 4: x + y# 5: In[1]# 6: In[1:4] # Slice notation worksAccessing Previous Outputs
Section titled “Accessing Previous Outputs”IPython also caches the results (outputs) of executed cells.
The Out Object and Underscore Variables
Section titled “The Out Object and Underscore Variables”Outputs are stored in a dictionary-like object called Out, where keys are the output cell numbers (Out[ ]) and values are the results. Note that commands that don’t produce an output (like assignments) won’t have an entry in Out.
# Continuing from the previous example...In [12]: OutOut[12]:{4: 30, 5: 'message = "Hello IPython"', 6: ['message = "Hello IPython"', 'x = 10', 'y = 20'], 7: 'In[1:4] # Slice notation works', 8: 'In[1]', 9: 'x + y'}
In [13]: Out[4]Out[13]: 30Similar to inputs, there are shorthand variables for the last three outputs:
_: Previous output.__: Output before the previous one.___: Output two results before the previous one.
In [14]: result = x * yOut[14]: 200
In [15]: _ # Previous output (from cell 14)Out[15]: 200
In [16]: print(f"The last result was: {_}")# Output: The last result was: 200
In [17]: __ # Output from cell 13Out[17]: 30
In [18]: ___ # Output from cell 12Out[18]:{4: 30, 5: 'message = "Hello IPython"', 6: ['message = "Hello IPython"', 'x = 10', 'y = 20'], 7: 'In[1:4] # Slice notation works', 8: 'In[1]', 9: 'x + y'}You can also access outputs using _N where N is the output cell number (e.g., _4 is equivalent to Out[4]).
In [19]: _4Out[19]: 30The _oh (output history) variable is another name for the Out dictionary.
Practical Uses
Section titled “Practical Uses”Input/output history is useful for:
- Debugging: Easily check previous inputs and outputs.
- Re-running commands: Use Up/Down arrows or
%history. - Reusing results: Assign outputs stored in
_,__,Out[N]to new variables. - Constructing new commands: Combine previous inputs/outputs in new expressions.
Understanding how to access and utilize session history is key to working efficiently in IPython and Jupyter.