Referring to the last expression

suggest change

To get the value of the last result from your last expression in the console, use an underscore \_.

>>> 2 + 2
4
>>> _
4
>>> _ + 6
10

This magic underscore value is only updated when using a python expression that results in a value. Defining functions or for loops does not change the value. If the expression raises an exception there will be no changes to \_.

>>> "Hello, {0}".format("World")
'Hello, World'
>>> _
'Hello, World'
>>> def wontchangethings():
...     pass
>>> _
'Hello, World'
>>> 27 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> _
'Hello, World'

Remember, this magic variable is only available in the interactive python interpreter. Running scripts will not do this.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents