Get last result in interactive Python shell
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In interactive Python sessions, quickly reusing previous outputs can speed up exploration, debugging, and ad-hoc calculations. Both standard Python REPL and IPython provide mechanisms to access recent results, but behavior differs across environments. Knowing these differences helps avoid confusing state bugs during notebook and terminal workflows.
Standard Python REPL: Underscore Variable
In CPython interactive mode, _ stores the last expression result.
This is convenient for short chains of calculations. However, _ can be overwritten easily by later expressions.
Important limitations
- '
_tracks only the most recent result' - assignment to
_changes its value manually - behavior is for interactive use, not scripts
So it is useful for quick experimentation but not durable state management.
IPython Enhancements: _, __, ___, and Out
IPython extends history access significantly.
- '
_last output' - '
__second last output' - '
___third last output' - '
Out[n]output of execution countn'
Example in IPython:
This makes iterative analysis much easier than plain REPL.
Jupyter Notebook Behavior
Jupyter notebooks use IPython kernel, so the same history variables apply. You can also reference outputs directly by execution count.
Be careful when re-running cells out of order. Execution counters reflect run order, not notebook visual order.
Why History Shortcuts Can Be Risky
History variables are convenient but can hide dependencies. If a notebook is restarted, Out cache is cleared and _ may not hold expected values.
Safer pattern for important results:
Explicit variable names improve reproducibility and readability, especially in shared notebooks.
Accessing Input History Too
IPython also tracks input history.
This command prints numbered input and output history. It helps reconstruct interactive sessions and audit what generated a given result.
For plain Python REPL, history behavior depends on environment and readline integration.
Practical Workflow Tips
Use history shortcuts for:
- quick arithmetic chaining
- ad-hoc transformation checks
- temporary diagnostics
Use named variables for:
- values used across multiple cells
- intermediate outputs in published notebooks
- anything required for later reproducible runs
This balance keeps exploration fast without sacrificing maintainability.
Clearing or Managing State
In IPython/Jupyter, stale history can confuse debugging. Useful commands:
This clears user namespace quickly. You can also restart kernel in notebooks to guarantee clean execution context.
For robust workflows, periodic kernel restarts plus run-all checks expose hidden dependencies on old interactive state.
Shell Differences to Remember
Environment behavior summary:
- CPython REPL has
_for last result - IPython adds multi-level output references and
Outdictionary - Jupyter mirrors IPython behavior but adds cell execution ordering complexity
Knowing your shell prevents surprises when moving commands between environments.
Debugging Unexpected _ Values
If _ is not what you expect:
- check whether another expression ran in between
- inspect if
_was assigned manually - in IPython, inspect
Outfor exact execution index values - restart session if state is unclear
Interactive speed is useful, but explicit state wins during debugging.
Common Pitfalls
- Relying on
_in long notebook workflows and losing track of provenance. - Assuming visual cell order matches execution history order.
- Using history variables in teaching or shared code where reproducibility matters.
- Forgetting that kernel restart clears output history state.
- Confusing standard REPL behavior with IPython-specific features.
Summary
- '
_gives the most recent result in interactive Python sessions.' - IPython and Jupyter provide richer access through
__,___, andOut[n]. - History shortcuts are excellent for exploration but fragile for reusable workflows.
- Use explicit variable names for reproducible analysis and collaboration.
- Restart and rerun workflows regularly to catch hidden state dependencies.
Related reading
- Get Latest Message for a Confluent Kafka Topic in Python
- Get lengths of a list in a jinja2 template
- Get list from pandas dataframe column or row?
- Get list of all routes defined in the Flask app
- Get list of pandas dataframe columns based on data type
- Get Month name from month number
- Get name of current script in Python
- Get node list from random walk in networkX
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.