diff --git a/Doc/tutorial/appendix.rst b/Doc/tutorial/appendix.rst index 241a812037469e..6d74137960d86c 100644 --- a/Doc/tutorial/appendix.rst +++ b/Doc/tutorial/appendix.rst @@ -31,6 +31,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, stored in the :mod:`builtins` +module. It stores the result of the last evaluated expression (*except* ``None`` +— it remains unchanged). + +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-05-02.bpo-41394.BEos8A.rst b/Misc/NEWS.d/next/Documentation/2020-07-27-23-05-02.bpo-41394.BEos8A.rst new file mode 100644 index 00000000000000..8ccfca2d549c0f --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2020-07-27-23-05-02.bpo-41394.BEos8A.rst @@ -0,0 +1,2 @@ +Added "Special ``_`` Variable" section to the "16. Appendix" part of Python tutorial. +It includes the definition of ``_`` and an example.