解释器使用
1. 启动与退出
python
>>> 2 + 2
4
>>> exit() # 或 quit() / Ctrl+D (Unix) / Ctrl+Z + Enter (Windows)2. 传入参数
通过命令行参数传递:
python -c "import sys; print(sys.argv)"
# ['-', ...]
python script.py arg1 arg2
# sys.argv = ['script.py', 'arg1', 'arg2']3. 交互模式
交互模式下,主提示符为 >>>,次提示符为 ...(多行结构):
>>> for i in range(3):
... print(i)
...
0
1
24. 源文件编码
Python 3 默认源文件编码 UTF-8,若需要其他编码可在文件首行声明:
# -*- coding: latin-1 -*-5. 注释
# 后到行尾为注释:
# 这是行注释
print("ok") # 行尾注释6. REPL 进阶技巧
help(obj):查看文档dir(obj):列出属性import this:Python 之禅import antigravity:彩蛋(打开漫画)- 输入
_:上一次结果
小结
REPL 是 Python 探索 API、调试代码的利器,下一章开始正式学习语法。