help()
?
?? 获取源代码
Tab键自动探索代码,快速补全
- 以%开始的函数,成为魔法函数,例如加了%的run可以运行外部函数
#在外部新建一个test.py文件,写入函数
def square(x):
return x ** 2
#运行外部函数
%run test.py
调用外部函数
square(2)
out: 4
- timeit 用来测试一行Python语句的执行时间
%timeit L = [n ** 2 for n in range(100)]
out:
22.5 µs ± 586 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#%%timeit 用来测试多行Python语句的执行时间
%%timeit
L = []
for n in range(100):
L.append(n ** 2)
out:
35 µs ± 690 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
# %debug 用来在交互环境中调试程序,运行报错后开启,打印一些参数,可以快速找到错误
%debug
# 查看所有魔法函数
%lsmagic
out:
Available line magics:
%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %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 %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%cmd %%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.
!cd
out:D:\python-project\data_analysis\Jupyter_learning
- 异常控制 %xmode:可以在轨迹追溯中找到错误的原因
# %xmode Plain 以紧凑的方式显示异常信息
# %xmode Verbose 会自动加入执行过程中的额外参数信息,错误信息更详细
# 默认 xmode Content