Ipython
一、启动程序
二、IPython的帮助文档
- 使用help()
- 使用?
- tab自动补全
三、IPython魔法命令
- 运行外部Python文件
- 运行计时
- 查看当前会话中的所有变量与函数
- 执行Linux指令
- 更多魔法命令
四、IPython输入输出历史
- 可使用In/Out调用输入输出历史
- 使用下划线表示输出
一、启动程序
执行以下命令:
jupyter notebook
[NotebookApp] Serving notebooks from local directory: /home/nanfengpo
[NotebookApp] 0 active kernels
[NotebookApp] The IPython Notebook is running at: http://localhost:8888/
[NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
注意以下几点:
- 打开地址为当前bash的目录,默认的根目录
- 浏览器地址为http://localhost:8888/
- 通过control -C终止jupyter程序
几个基本操作:
- 双击D:删除当前cell
- 单击M:转为markdown文档
- markdown文档下运行变为预览模式
二、IPython的帮助文档
1. 使用help()
通过以下命令来获得帮助文档:
help(len)
Help on built-in function len in module builtins:
len(obj, /) Return the number of items in a container.
In [1]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000">help</span>(<span style="color:#008000">len</span>)</span></span></span>
Help on built-in function len in module builtins:
len(obj, /)
Return the number of items in a container.
2. 使用?
或者使用问号:
len?
In [2]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000">len</span><span style="color:#aa22ff"><strong>?</strong></span></span></span></span>
Type Markdown and LaTeX: ????2α2
还可以应用到自定义的变量和自定义的函数上来返回帮助文档
此外,使用两个??可以把函数的源代码显示出来
3. tab自动补全
敲击tab键能自动补全
L.
In [3]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000"><strong>import</strong></span> <span style="color:#000000">numpy</span></span></span></span>
也可以在import的时候自动补全
import nu
三、IPython魔法命令
1. 运行外部Python文件
使用下面命令运行外部python文件(默认是当前目录,最好加上绝对路径)
%run *.py
例如在当前目录下有一个myscript.py文件:
def square(x): """square a number""" return x ** 2
for N in range(1, 4): print(N, "squared is", square(N))
In [11]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#aa22ff"><strong>%</strong></span><span style="color:#000000">run</span> <span style="color:#000000">test</span>.py</span></span></span>
来自外部的文件
我们可以通过下面命令执行它:
%run myscript.py
尤其要注意的是,当我们使用魔法命令执行了一个外部文件时,该文件的函数就能在当前会话中使用
square(5)
In [12]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#000000">my_func1</span>()</span></span></span>
0
1
2
3
4
2. 运行计时
用下面命令计算statement的运行时间:
%time statement
In [13]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#aa22ff"><strong>%</strong></span><span style="color:#000000">time</span> <span style="color:#008000">print</span>(<span style="color:#ba2121">'hello world'</span>)</span></span></span>
hello world
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 56 µs
用下面命令计算statement的平均运行时间:
%timeit statement
timeit会多次运行statement,最后得到一个更为精准的预期运行时间
可以使用两个百分号来测试多行代码的平均运行时间:
` %%timeit
statement1
statement2
statement3
`
记住:
- %time一般用于耗时长的代码段
- %timeit一般用于耗时短的代码段
3. 查看当前会话中的所有变量与函数
快速查看当前会话的所有变量与函数名称:
%who
In [1]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#000000">a</span>,<span style="color:#000000">b</span> <span style="color:#aa22ff"><strong>=</strong></span> <span style="color:#008800">19</span>,<span style="color:#008800">40</span></span></span></span>
In [2]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#aa22ff"><strong>%</strong></span><span style="color:#000000">who</span></span></span></span>
a b
查看当前会话的所有变量与函数名称的详细信息:
%whos
In [3]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#aa22ff"><strong>%</strong></span><span style="color:#000000">whos</span></span></span></span>
Variable Type Data/Info
----------------------------
a int 19
b int 40
In [43]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#aa22ff"><strong>%</strong></span><span style="color:#000000">whos</span></span></span></span>
Variable Type Data/Info
-----------------------------------
function2 function <function function2 at 0x7fb7538d6950>
i int 0
my_func function <function my_func at 0x7fb7538d6730>
my_func2 function <function my_func2 at 0x7fb7538d67b8>
my_function function <function my_function at 0x7fb78831d6a8>
np module <module 'numpy' from '/us<...>kages/numpy/__init__.py'>
pd module <module 'pandas' from '/u<...>ages/pandas/__init__.py'>
sum int 0
返回一个字符串列表,里面元素是当前会话的所有变量与函数名称:
%who_ls
4. 执行Linux指令
Linux指令:
$ echo "hello world" # echo is like Python's print function hello world
$ pwd # pwd = print working directory /home/jake # this is the "path" that we're sitting in
$ ls # ls = list working directory contents notebooks projects
$ mkdir mm /home/jake/projects
在Linux指令之前加上 !,即可在ipython当中执行Linux指令。
注意会将标准输出以字符串形式返回
In [17]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#aa22ff"><strong>!</strong></span><span style="color:#000000">pwd</span></span></span></span>
/home/nanfengpo/Desktop/data_analysis/day01/target
5. 更多魔法命令
列出所有魔法命令
lsmagic
In [20]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#000000">lsmagic</span></span></span></span>
Out[20]:
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
查看魔法命令的文档: 使用?
四、IPython输入输出历史
1. 可使用In/Out调用输入输出历史
In返回一个字符串列表,里面是所有输入命令的字符串
Out返回一个含有输出的命令的序号及其输出组成的字典
两者皆可以通过索引获取元素
In [23]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000"><strong>for</strong></span> <span style="color:#000000">item</span> <span style="color:#008000"><strong>in</strong></span> <span style="color:#008000">range</span>(<span style="color:#008800">1</span>,<span style="color:#008800">6</span>):</span></span></span>
<span style="color:#000000"><span style="color:black"><span style="color:inherit"> <span style="color:#008000">print</span>(<span style="color:#000000">item</span>)</span></span></span>
1
2
3
4
5
2. 使用下划线表示输出
"_"表示上一个输出
"_2"表示Out[2]
五、notebook的快捷键
1、命令模式
• Enter : 转入编辑模式
• Shift-Enter : 运行本单元,选中下个单元
• Ctrl-Enter : 运行本单元,选中当前单元
• Alt-Enter : 运行本单元,在下面插入一单元
In [31]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#408080"><em># title</em></span></span></span></span>
In [ ]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"></span></span></span>
• Y : 单元转入代码状态
• M :单元转入markdown状态
• A : 在上方插入新单元
• B : 在下方插入新单元
2、编辑模式 ( Enter 键启动)
• Tab : 代码补全或缩进
• Shift-Tab : 提示
In [33]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000"><strong>import</strong></span> <span style="color:#000000">numpy</span> <span style="color:#008000"><strong>as</strong></span> <span style="color:#000000">np</span></span></span></span>
In [ ]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#000000">np</span>.zeros()</span></span></span>
• Ctrl-A : 全选
• Ctrl-Z : 复原
============================================
练习:
在Jupyter上实现以前的代码,包括:
- 简单代码
- 分支
- 循环
- 函数
- 类
============================================
In [35]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#000000">xmax</span>,<span style="color:#000000">xmin</span> <span style="color:#aa22ff"><strong>=</strong></span> <span style="color:#008800">50</span>,<span style="color:#008800">10</span></span></span></span>
In [36]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000"><strong>if</strong></span> <span style="color:#000000">xmax</span><span style="color:#aa22ff"><strong>></strong></span><span style="color:#008800">40</span>:</span></span></span>
<span style="color:#000000"><span style="color:black"><span style="color:inherit"> <span style="color:#008000">print</span>(<span style="color:#ba2121">'非常大'</span>)</span></span></span>
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000"><strong>else</strong></span>:</span></span></span>
<span style="color:#000000"><span style="color:black"><span style="color:inherit"> <span style="color:#008000">print</span>(<span style="color:#ba2121">'一般大'</span>)</span></span></span>
非常大
In [37]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000"><strong>for</strong></span> <span style="color:#000000">i</span> <span style="color:#008000"><strong>in</strong></span> [<span style="color:#008800">1</span>,<span style="color:#008800">2</span>,<span style="color:#008800">3</span>,<span style="color:#008800">4</span>,<span style="color:#008800">58</span>,<span style="color:#008800">9</span>,<span style="color:#008800">10</span>]:</span></span></span>
<span style="color:#000000"><span style="color:black"><span style="color:inherit"> <span style="color:#008000">print</span>(<span style="color:#000000">i</span>)</span></span></span>
. . .
In [ ]:
<span style="color:#000000"><span style="color:black"><span style="color:inherit"><span style="color:#008000"><strong>def</strong></span> <span style="color:#0000ff">transform</span>(<span style="color:#000000">number</span>):</span></span></span>
<span style="color:#000000"><span style="color:black"><span style="color:inherit"> <span style="color:#008000">print</span>(<span style="color:#ba2121">'this is a number of %d'</span><span style="color:#aa22ff"><strong>%</strong></span>(<span style="color:#000000">number</span>))</span></span></span>