3. 关键字与内置函数
关键字(Keywords)
通过 kwlist 导入的关键字列表,这些是 Python 语言中具有特殊功能的核心保留字,例如:if、else、for、while、def、class、import、from、try、except 等。这些词在 Python 语法中有特定用途,不能用作普通标识符。
软关键字(Soft Keywords)
通过 softkwlist 导入的软关键字列表,这些词在特定上下文中具有特殊含义,但在其他情况下可以作为普通标识符使用。例如:match、case(用于模式匹配语法)。它们只在特定的语法结构中被保留。
python
from keyword import kwlist, softkwlist
# 遍历 kwlist 关键字列表
for kw in kwlist:
print(kw, end='\t')
print('-' * 30)
# 遍历 softkwlist 软关键字列表
for kw in softkwlist:
print(kw, end='\t')
print('-' * 30)
▶ 运行结果:
输出
False None True and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield ------------------------------
_ case match type ------------------------------
Python 系统内置函数
下列是不建议当做变量名称的 Python 系统内置函数,若是不小心将系统内置函数名称当做变量,程序本身不会错误,但是原先函数功能会丧失。Python 共有 68 个内置函数。
| 常用内置函数 | 说明 |
|---|---|
abs() max() min() sum() round() pow() | 数学运算 |
len() range() enumerate() zip() | 序列操作 |
int() float() str() list() dict() set() tuple() | 类型转换 |
print() input() open() | 输入输出 |
sorted() reversed() map() filter() | 排序与过滤 |
type() isinstance() dir() help() id() | 类型与属性 |
chr() ord() bin() hex() oct() | 编码转换 |
eval() exec() callable() hash() | 执行与判断 |