|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PDSH-cover-small.png\">\n", |
| 8 | + "*This notebook contains an excerpt from the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook).*\n", |
| 9 | + "\n", |
| 10 | + "*The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please support the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "# Input and Output History\n", |
| 18 | + "\n", |
| 19 | + "Previously we saw that the IPython shell allows you to access previous commands with the up and down arrow keys, or equivalently the Ctrl-p/Ctrl-n shortcuts.\n", |
| 20 | + "Additionally, in both the shell and the notebook, IPython exposes several ways to obtain the output of previous commands, as well as string versions of the commands themselves.\n", |
| 21 | + "We'll explore those here." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "## IPython's ``In`` and ``Out`` Objects\n", |
| 29 | + "\n", |
| 30 | + "By now I imagine you're quite familiar with the ``In [1]:``/``Out[1]:`` style prompts used by IPython.\n", |
| 31 | + "But it turns out that these are not just pretty decoration: they give a clue as to how you can access previous inputs and outputs in your current session.\n", |
| 32 | + "Imagine you start a session that looks like this:\n", |
| 33 | + "\n", |
| 34 | + "```ipython\n", |
| 35 | + "In [1]: import math\n", |
| 36 | + "\n", |
| 37 | + "In [2]: math.sin(2)\n", |
| 38 | + "Out[2]: 0.9092974268256817\n", |
| 39 | + "\n", |
| 40 | + "In [3]: math.cos(2)\n", |
| 41 | + "Out[3]: -0.4161468365471424\n", |
| 42 | + "```" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "We've imported the built-in ``math`` package, then computed the sine and the cosine of the number 2.\n", |
| 50 | + "These inputs and outputs are displayed in the shell with ``In``/``Out`` labels, but there's more–IPython actually creates some Python variables called ``In`` and ``Out`` that are automatically updated to reflect this history:\n", |
| 51 | + "\n", |
| 52 | + "```ipython\n", |
| 53 | + "In [4]: print(In)\n", |
| 54 | + "['', 'import math', 'math.sin(2)', 'math.cos(2)', 'print(In)']\n", |
| 55 | + "\n", |
| 56 | + "In [5]: Out\n", |
| 57 | + "Out[5]: {2: 0.9092974268256817, 3: -0.4161468365471424}\n", |
| 58 | + "```" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "The ``In`` object is a list, which keeps track of the commands in order (the first item in the list is a place-holder so that ``In[1]`` can refer to the first command):\n", |
| 66 | + "\n", |
| 67 | + "```ipython\n", |
| 68 | + "In [6]: print(In[1])\n", |
| 69 | + "import math\n", |
| 70 | + "```\n", |
| 71 | + "\n", |
| 72 | + "The ``Out`` object is not a list but a dictionary mapping input numbers to their outputs (if any):\n", |
| 73 | + "\n", |
| 74 | + "```ipython\n", |
| 75 | + "In [7]: print(Out[2])\n", |
| 76 | + "0.9092974268256817\n", |
| 77 | + "```\n", |
| 78 | + "\n", |
| 79 | + "Note that not all operations have outputs: for example, ``import`` statements and ``print`` statements don't affect the output.\n", |
| 80 | + "The latter may be surprising, but makes sense if you consider that ``print`` is a function that returns ``None``; for brevity, any command that returns ``None`` is not added to ``Out``.\n", |
| 81 | + "\n", |
| 82 | + "Where this can be useful is if you want to interact with past results.\n", |
| 83 | + "For example, let's check the sum of ``sin(2) ** 2`` and ``cos(2) ** 2`` using the previously-computed results:\n", |
| 84 | + "\n", |
| 85 | + "```ipython\n", |
| 86 | + "In [8]: Out[2] ** 2 + Out[3] ** 2\n", |
| 87 | + "Out[8]: 1.0\n", |
| 88 | + "```\n", |
| 89 | + "\n", |
| 90 | + "The result is ``1.0`` as we'd expect from the well-known trigonometric identity.\n", |
| 91 | + "In this case, using these previous results probably is not necessary, but it can become very handy if you execute a very expensive computation and want to reuse the result!" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "## Underscore Shortcuts and Previous Outputs\n", |
| 99 | + "\n", |
| 100 | + "The standard Python shell contains just one simple shortcut for accessing previous output; the variable ``_`` (i.e., a single underscore) is kept updated with the previous output; this works in IPython as well:\n", |
| 101 | + "\n", |
| 102 | + "```ipython\n", |
| 103 | + "In [9]: print(_)\n", |
| 104 | + "1.0\n", |
| 105 | + "```\n", |
| 106 | + "\n", |
| 107 | + "But IPython takes this a bit further—you can use a double underscore to access the second-to-last output, and a triple underscore to access the third-to-last output (skipping any commands with no output):\n", |
| 108 | + "\n", |
| 109 | + "```ipython\n", |
| 110 | + "In [10]: print(__)\n", |
| 111 | + "-0.4161468365471424\n", |
| 112 | + "\n", |
| 113 | + "In [11]: print(___)\n", |
| 114 | + "0.9092974268256817\n", |
| 115 | + "```\n", |
| 116 | + "\n", |
| 117 | + "IPython stops there: more than three underscores starts to get a bit hard to count, and at that point it's easier to refer to the output by line number.\n", |
| 118 | + "\n", |
| 119 | + "There is one more shortcut we should mention, however–a shorthand for ``Out[X]`` is ``_X`` (i.e., a single underscore followed by the line number):\n", |
| 120 | + "\n", |
| 121 | + "```ipython\n", |
| 122 | + "In [12]: Out[2]\n", |
| 123 | + "Out[12]: 0.9092974268256817\n", |
| 124 | + "\n", |
| 125 | + "In [13]: _2\n", |
| 126 | + "Out[13]: 0.9092974268256817\n", |
| 127 | + "```" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "markdown", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "## Suppressing Output\n", |
| 135 | + "Sometimes you might wish to suppress the output of a statement (this is perhaps most common with the plotting commands that we'll explore in [Introduction to Matplotlib](04.00-Introduction-To-Matplotlib.ipynb)).\n", |
| 136 | + "Or maybe the command you're executing produces a result that you'd prefer not like to store in your output history, perhaps so that it can be deallocated when other references are removed.\n", |
| 137 | + "The easiest way to suppress the output of a command is to add a semicolon to the end of the line:\n", |
| 138 | + "\n", |
| 139 | + "```ipython\n", |
| 140 | + "In [14]: math.sin(2) + math.cos(2);\n", |
| 141 | + "```\n", |
| 142 | + "\n", |
| 143 | + "Note that the result is computed silently, and the output is neither displayed on the screen or stored in the ``Out`` dictionary:\n", |
| 144 | + "\n", |
| 145 | + "```ipython\n", |
| 146 | + "In [15]: 14 in Out\n", |
| 147 | + "Out[15]: False\n", |
| 148 | + "```" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "markdown", |
| 153 | + "metadata": {}, |
| 154 | + "source": [ |
| 155 | + "## Related Magic Commands\n", |
| 156 | + "For accessing a batch of previous inputs at once, the ``%history`` magic command is very helpful.\n", |
| 157 | + "Here is how you can print the first four inputs:\n", |
| 158 | + "\n", |
| 159 | + "```ipython\n", |
| 160 | + "In [16]: %history -n 1-4\n", |
| 161 | + " 1: import math\n", |
| 162 | + " 2: math.sin(2)\n", |
| 163 | + " 3: math.cos(2)\n", |
| 164 | + " 4: print(In)\n", |
| 165 | + "```\n", |
| 166 | + "\n", |
| 167 | + "As usual, you can type ``%history?`` for more information and a description of options available.\n", |
| 168 | + "Other similar magic commands are ``%rerun`` (which will re-execute some portion of the command history) and ``%save`` (which saves some set of the command history to a file).\n", |
| 169 | + "For more information, I suggest exploring these using the ``?`` help functionality discussed in [Help and Documentation in IPython](01.01-Help-And-Documentation.ipynb)." |
| 170 | + ] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "anaconda-cloud": {}, |
| 175 | + "kernelspec": { |
| 176 | + "display_name": "Python [default]", |
| 177 | + "language": "python", |
| 178 | + "name": "python3" |
| 179 | + }, |
| 180 | + "language_info": { |
| 181 | + "codemirror_mode": { |
| 182 | + "name": "ipython", |
| 183 | + "version": 3 |
| 184 | + }, |
| 185 | + "file_extension": ".py", |
| 186 | + "mimetype": "text/x-python", |
| 187 | + "name": "python", |
| 188 | + "nbconvert_exporter": "python", |
| 189 | + "pygments_lexer": "ipython3", |
| 190 | + "version": "3.5.1" |
| 191 | + } |
| 192 | + }, |
| 193 | + "nbformat": 4, |
| 194 | + "nbformat_minor": 0 |
| 195 | +} |
0 commit comments