English 中文(简体)
IPython - IO Caching
  • 时间:2024-11-03

IPython - IO Caching


Previous Page Next Page  

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

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 psts 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 ]

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 spcing 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)
Advertisements