Seeing the result of the last expression

When you type an expression by itself in interactive mode, or when Python returns an expression as a result of something you typed, Python also stores the value of the expression in a special name: _ (an underscore character). This name is available only in interactive mode. To see the value stored, type _.

TECHNICAL Note that _ doesn't store the results of any statements (assignments STUFF such as x=2 5 or commands such as print). In the following example,

_ continues to hold the value of the expression even after a statement was typed:

>>> "Hello, Nurse!" 'Hello, Nurse!'

Warning Don't rely on _ in long segments of code. The value stored in _ may change unexpectedly if you aren't paying close attention to the difference between statements and expressions.

Was this article helpful?

0 0

Post a comment