print("Hello World!") # 注释,不会被执行
Hello World!
a = 2 # 赋值语句
单独的变量值会被输出:
a
2
for idx in range(3): # 多行代码第一行
print(idx) # 多行代码第二行
0 1 2
脚本模式需要将Python代码写入一个文本文件来运行。将上文解释器模式中使用的代码,写入一个文本文件,命名为“test.py”,其内容为:
print("Hello World!") # 一些注释,不会被执行
a = 2
a
for idx in range(3):
print(idx)
使用魔术命令写入:
%%writefile test.py
print("Hello World!") # 一些注释,不会被执行
a = 2
a
for idx in range(3):
print(idx)
Writing test.py
使用命令行执行:
!python test.py
Hello World! 0 1 2