Use The Underscore in The REPL

You can obtain the result of the last expression in a Python REPL with the underscore operator, e.g. in the Python REPL this looks like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> 3 * 3
9
>>> _ + 3
12
>>> 3 * 3 9 >>> _ + 3 12
>>> 3 * 3
9
>>> _ + 3
12

This works in the IPython shell too. In addition, the IPython shell allows you to use Out[n] to get the value of the expression In[n]. E.g., Out[1] would give us the number 9 in the example below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
In [1]: 3 * 3
Out[1]: 9
In [2]: Out[1] + 3
Out[2]: 12
In [3]: _ + 3
Out[3]: 15
In [1]: 3 * 3 Out[1]: 9 In [2]: Out[1] + 3 Out[2]: 12 In [3]: _ + 3 Out[3]: 15
In [1]: 3 * 3
Out[1]: 9

In [2]: Out[1] + 3
Out[2]: 12

In [3]: _ + 3
Out[3]: 15

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

Leave a Comment