
1.IPython 的帮助文档
-
使用 help()
-
使用 ?
-
使用 ??
-
tab 自动补全
-
shift + tab 查看参数和函数说明
2.运行外部 Python 文件
-
使用下面命令运行外部 Python 文件(默认是当前目录,也可以使用绝对路径)
%run *.py
示例:在当前目录下有一个 myscript.py 文件:
def square(x):"""square a number"""return x ** 2for N in range(1,4):print(N, "squared is", square(N))
可以通过下面命令执行:
%run myscript.py# 执行效果1 squared is 12 squared is 43 squared is 9
尤其要注意的是,当我们使用魔法命令执行了一个外部文件时,该文件的函数就能在当前会话中使用
-
只输出最后一个
square(5)square(6)# 执行结果36
-
a,b只能写在最后一行,以元组输出
a = square(5)b = square(6)a,b# 执行结果(25, 36)
-
换行分别输出
a = square(5)b = square(6)display(a,b)# 执行结果2536
-
以打印的方式输出
a = square(5)b = square(6)print(a,b)# 执行结果25 36
-
导入 myscript.py 文件
import myscript#执行效果1 squared is 12 squared is 43 squared is 9
3.运行计时
-
用下面命令计算 statement 的运行时间
%time statement# %time:一般用来统计耗时较长代码的运行时长%time square(100)# 执行效果CPU times: total: 0 nsWall time: 0 nsOut[28]:10000
-
用下面命令统计 statement 的平均运行时间
%timeit statement
-
timeit 会多次运行 statement,最后得到一个更为精准的预期运行时间
%timeit square(100)#执行效果209 ns ± 8.52 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)%timeit -r 3 -n 100 square(100)#执行效果322 ns ± 45.9 ns per loop (mean ± std. dev. of 3 runs, 100 loops each)
-
可以使用两个百分号来测试多行代码的平均运行时间
%%timeitsquare(100)add(10,20)# 执行效果344 ns ± 5.84 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
-
记住
%time # 一般用于耗时长的代码段%timeit # 一般用于耗时短的代码段
4.查看当前会话中的所有变量与函数(了解)
-
快速查看当前会话的所有变量与函数名称
%who# 执行效果N a add b myscript numpy square
-
查看当前会话的所有变量与函数名称的详细信息
%whos# 执行效果Variable Type Data/Info--------------------------------N int 3a int 25add function <function add at 0x000001AD7CAEE0C0>b int 36myscript module <module 'myscript' from '<...>top\\NumPy\\myscript.py'>numpy module <module 'numpy' from 'D:\<...>ges\\numpy\\__init__.py'>square function <function square at 0x000001AD7D8F9C60>
-
返回一个字符串列表,里面元素时当前会话的所有变量与函数名称
%who_ls# 执行效果['N', 'a', 'add', 'b', 'myscript', 'numpy', 'square']
4.安装包
-
使用 pip 命令安装
pip install numpy
5.更多魔法命令
-
列出所有魔法命令
lsmagic# 执行效果Available line magics:%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %code_wrap %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmodeAvailable cell magics:%%! %%HTML %%SVG %%bash %%capture %%cmd %%code_wrap %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefileAutomagic is ON, % prefix IS NOT needed for line magics.
-
查看魔法命令的文档:使用?
# 例如查看魔法命令 %run 的文档%run?



















