Jupyter (IPython) の便利機能

入力や出力の参照

x, y = 10, 5
x + y
15
x - y
5
x * y
50
x / y
2.0
_, __, ___
(2.0, 50, 5)
In[2]
'x + y'
Out[2]
15

シェルコマンドの実行

!ls *.txt
prefecture.txt	 prefecture3.txt  requirements.txt
prefecture2.txt  prefecture4.txt  tokyo-covid.txt
!cat prefecture.txt
東京 とうきょう Tokyo
神奈川 かながわ Kanagawa
千葉 ちば Chiba
埼玉 さいたま Saitama
!cut -f2 -d" " prefecture.txt
とうきょう
かながわ
ちば
さいたま
yomi = !cut -f2 -d" " prefecture.txt
yomi
['とうきょう', 'かながわ', 'ちば', 'さいたま']

イントロスペクション

print?
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Type:      builtin_function_or_method
str.format?
Docstring:
S.format(*args, **kwargs) -> str

Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
Type:      method_descriptor

実行速度の計測

%timeit sum(range(1000))
11.1 µs ± 674 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit sum([x for x in range(1000)])
24.3 µs ± 1.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit sum(x for x in range(1000))
31 µs ± 298 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%%timeit
s = 0
for i in range(1000):
    s += i
31.4 µs ± 288 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

デバッグ

def count_upper(s):
    n = 0
    for c in s:
        if c.isupper():
            ++n
    return n
count_upper('New York')
0
%pdb on
Automatic pdb calling has been turned ON
def count_upper(s):
    breakpoint()
    n = 0
    for c in s:
        if c.isupper():
            ++n
    return n
count_upper('New York')
> <ipython-input-23-e2234f85ac9f>(3)count_upper()
      1 def count_upper(s):
      2     breakpoint()
----> 3     n = 0
      4     for c in s:
      5         if c.isupper():

ipdb>  n
> <ipython-input-23-e2234f85ac9f>(4)count_upper()
      2     breakpoint()
      3     n = 0
----> 4     for c in s:
      5         if c.isupper():
      6             ++n

ipdb>  n
> <ipython-input-23-e2234f85ac9f>(5)count_upper()
      3     n = 0
      4     for c in s:
----> 5         if c.isupper():
      6             ++n
      7     return n

ipdb>  n
> <ipython-input-23-e2234f85ac9f>(6)count_upper()
      3     n = 0
      4     for c in s:
      5         if c.isupper():
----> 6             ++n
      7     return n

ipdb>  n
> <ipython-input-23-e2234f85ac9f>(4)count_upper()
      2     breakpoint()
      3     n = 0
----> 4     for c in s:
      5         if c.isupper():
      6             ++n

ipdb>  p n
0
ipdb>  quit
---------------------------------------------------------------------------
BdbQuit                                   Traceback (most recent call last)
<ipython-input-24-ad74a7582071> in <module>
----> 1 count_upper('New York')

<ipython-input-23-e2234f85ac9f> in count_upper(s)
      2     breakpoint()
      3     n = 0
----> 4     for c in s:
      5         if c.isupper():
      6             ++n

<ipython-input-23-e2234f85ac9f> in count_upper(s)
      2     breakpoint()
      3     n = 0
----> 4     for c in s:
      5         if c.isupper():
      6             ++n

~/anaconda3/lib/python3.8/bdb.py in trace_dispatch(self, frame, event, arg)
     86             return # None
     87         if event == 'line':
---> 88             return self.dispatch_line(frame)
     89         if event == 'call':
     90             return self.dispatch_call(frame, arg)

~/anaconda3/lib/python3.8/bdb.py in dispatch_line(self, frame)
    111         if self.stop_here(frame) or self.break_here(frame):
    112             self.user_line(frame)
--> 113             if self.quitting: raise BdbQuit
    114         return self.trace_dispatch
    115 

BdbQuit: 
> /home/okazaki/anaconda3/lib/python3.8/bdb.py(113)dispatch_line()
    111         if self.stop_here(frame) or self.break_here(frame):
    112             self.user_line(frame)
--> 113             if self.quitting: raise BdbQuit
    114         return self.trace_dispatch
    115 

ipdb>  quit
sum(1 for c in 'New York' if c.isupper())
2