Python
Interactive Shell
Coding Tips
Python Programming
Python Tricks

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.

Browse interview questions

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.

python
1>>> 10 * 3
230
3>>> _ + 5
435
5>>> _
635

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 count n'

Example in IPython:

python
1In [1]: 2 + 2
2Out[1]: 4
3
4In [2]: 10 * 10
5Out[2]: 100
6
7In [3]: _
8Out[3]: 100
9
10In [4]: __
11Out[4]: 4
12
13In [5]: Out[1]
14Out[5]: 4

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.

python
# In a later cell
Out[7]

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:

python
result = expensive_computation()

Explicit variable names improve reproducibility and readability, especially in shared notebooks.

Accessing Input History Too

IPython also tracks input history.

python
In [1]: %history -n -o

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:

python
%reset -f

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 Out dictionary
  • 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:

  1. check whether another expression ran in between
  2. inspect if _ was assigned manually
  3. in IPython, inspect Out for exact execution index values
  4. 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 __, ___, and Out[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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.