The Python Oracle

Get last result in interactive Python shell

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC L Beethoven - Piano Sonata No 8 in C

--

Chapters
00:00 Question
00:24 Accepted answer (Score 299)
00:37 Answer 2 (Score 105)
01:19 Answer 3 (Score 20)
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/2000...

Answer 2 links:
https://jakevdp.github.io/PythonDataScie...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 317


Underscore.

>>> 5+5
10
>>> _
10
>>> _ + 5
15
>>> _
15



ANSWER 2

Score 107


Just for the record, ipython takes this one step further and you can access every result with _ and its numeric value

In [1]: 10
Out[1]: 10

In [2]: 32
Out[2]: 32

In [3]: _
Out[3]: 32

In [4]: _1
Out[4]: 10

In [5]: _2
Out[5]: 32

In [6]: _1 + _2
Out[6]: 42

In [7]: _6
Out[7]: 42

And it is possible to edit ranges of lines with the %ed macro too:

In [1]: def foo():
   ...:     print "bar"
   ...:     
   ...:     

In [2]: foo()
bar

In [3]: %ed 1-2



ANSWER 3

Score 23


IPython allows you to go beyond the single underscore _ with double (__) and triple underscore (___), returning results of the second- and third-to-last commands.

Alternatively, you can also use Out[n], where n is the number of the input that generated the output:

In [64]: 1+1
Out[64]: 2

...

In [155]: Out[64] + 3
Out[155]: 5

For more info, see https://jakevdp.github.io/PythonDataScienceHandbook/01.04-input-output-history.html .