在Jupyter notebook中,"魔法命令"是特殊的命令,不是Python语言的一部分,但可以使您的生活更轻松。这些命令前面有%符号。
魔法命令很有用,可以直接嵌入到python代码中,并解决常见问题,例如列出当前目录中的所有文件或更改当前工作目录。
%magic
:显示所有可用的魔术命令。
python
%magic

%quickref
:显示IPython快速参考卡。
python
%quickref

?
:查看魔术命令的帮助。
python
?

%who
:显示交互式空间中所有变量。
python
%who

%reset
:删除交互式空间中的所有变量。

%xdel
:删除交互式空间中的某个变量。

%run
:在当前内核中运行python脚本。
python
# 需要重新导入lab.py要使用的包和变量
%run lab.py
# 共享包,变量
%run -i lab.py
%time
:计算一行Python语句的运行时间。
python
%time
print("Hello World")

%%timeit
:计算一段python语句的运行时间。
python
%%timeit
print("Hello World")
