Jupyter 简明教程

IPython - IO Caching

IPython 控制台上的输入和输出单元按序编号。在本章中,我们将详细了解 Python 中的 IO 缓存。

The input and output cells on IPython console are numbered incrementally. In this chapter, let us look into IO caching in Python in detail.

在 IPython 中,使用向上箭头键检索输入。此外,所有先前的输入均已保存且可以检索。变量 _i_i__i 始终存储前三个输入条目。此外, In_in 变量提供所有输入的列表。显然, _in[n] 从第 n 个输入单元检索输入。以下 IPython 会话将帮助您理解这一现象 −

In IPython, inputs are retrieved using up arrow key. Besides, all previous inputs are saved and can be retrieved. The variables _i, _i, and __i always store the previous three input entries. In addition, In and _in variables provides lists of all inputs. Obviously _in[n] retrieves input from nth input cell. The following IPython session helps you to understand this phenomenon −

In [1]: print ("Hello")
Hello

In [2]: 2+2
Out[2]: 4

In [3]: x = 10

In [4]: y = 2

In [5]: pow(x,y)
Out[5]: 100

In [6]: _iii, _ii, _i
Out[6]: ('x = 10', 'y = 2', 'pow(x,y)')

In [7]: In
Out[7]:
['',
   'print ("Hello")',
   '2+2',
   'x = 10',
   'y = 2',
   'pow(x,y)',
   '_iii, _ii, _i',
   'In'
]

In [8]: In[5] 9. IPython — IO
Out[8]: 'pow(x,y)'

In [9]: _ih
Out[9]:
['',
   'print ("Hello")',
   '2+2',
   'x = 10',
   'y = 2',
   'pow(x,y)',
   '_iii, _ii, _i',
   'In',
   'In[5]',
   '_ih'
]

In [11]: _ih[4]
Out[11]: 'y = 2'

In [12]: In[1:4]
Out[12]: ['print ("Hello")', '2+2', 'x=10']

类似地,单下划线、双下划线和三下划线用作变量以存储前三个输出。 Out_oh 还形成单元格编号和执行操作的单元格输出的词典对象(不包括赋值语句)。要检索特定输出单元格的内容,请使用 Out[n]_oh[n] 。您还可以使用切片获取范围内的输出单元格。

Similarly, single, double and triple underscores act as variables to store previous three outputs. Also Out and _oh form a dictionary object of cell number and output of cells performing action (not including assignment statements). To retrieve contents of specific output cell, use Out[n] or _oh[n]. You can also use slicing to get output cells within a range.

In [1]: print ("Hello")
Hello

In [2]: 2+2
Out[2]: 4

In [3]: x = 10

In [4]: y = 3

In [5]: pow(x,y)
Out[5]: 1000

In [6]: ___, __, _
Out[6]: ('', 4, 1000)

In [7]: Out
Out[7]: {2: 4, 5: 1000, 6: ('', 4, 1000)}

In [8]: _oh
Out[8]: {2: 4, 5: 1000, 6: ('', 4, 1000)}

In [9]: _5
Out[9]: 1000

In [10]: Out[6]
Out[10]: ('', 4, 1000)