diff --git a/Doc/tutorial/appendix.rst b/Doc/tutorial/appendix.rst index 6a1611afadb57c..f8820141dd83ff 100644 --- a/Doc/tutorial/appendix.rst +++ b/Doc/tutorial/appendix.rst @@ -53,6 +53,32 @@ Typing an interrupt while a command is executing raises the :exc:`KeyboardInterrupt` exception, which may be handled by a :keyword:`try` statement. +.. _tut-var: + +Special ``_`` variable +------------------------- + +There is a special variable ``_`` in interactive mode, which stores the result of the last +evaluated expression. If the last expression evaluated to ``None``, then the value of ``_`` +is not updated. + +For example: + + >>> _ # Not defined yet + Traceback (most recent call last): + File "", line 1, in + _ + NameError: name '_' is not defined + >>> 1 + 2 + 3 + >>> _ + 3 + >>> print("1+2=3") + 1+2=3 + >>> _ # Doesn't store None returned by print() + 3 + >>> _ + 4 + 7 .. _tut-scripts: diff --git a/Misc/NEWS.d/next/Documentation/2020-07-27-23-02-27.bpo-41394.BEos8A.rst b/Misc/NEWS.d/next/Documentation/2020-07-27-23-02-27.bpo-41394.BEos8A.rst new file mode 100644 index 00000000000000..1da80580bf4de4 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2020-07-27-23-02-27.bpo-41394.BEos8A.rst @@ -0,0 +1,3 @@ +Added "Special ``_`` Variable" section to the "16. Appendix" part of Python tutorial. + +It includes the define of ``_`` and an example.