附录 A Python 内置函数速查表
Python 3.13 共有 71 个内置函数,按功能分类如下。
类型转换
| 函数 | 说明 | 示例 |
|---|
int(x) | 转为整数 | int("42") → 42 |
float(x) | 转为浮点数 | float("3.14") → 3.14 |
complex(r, i) | 创建复数 | complex(3, 4) → (3+4j) |
bool(x) | 转为布尔 | bool(0) → False |
str(x) | 转为字符串 | str(42) → "42" |
bytes(x) | 转为字节串 | bytes(5) → b'\x00\x00\x00\x00\x00' |
bytearray(x) | 可变字节串 | bytearray(b"hi") |
list(x) | 转为列表 | list("abc") → ['a','b','c'] |
tuple(x) | 转为元组 | tuple([1,2,3]) → (1,2,3) |
dict(**kw) | 创建字典 | dict(a=1, b=2) |
set(x) | 转为集合 | set([1,2,2]) → {1,2} |
frozenset(x) | 不可变集合 | frozenset([1,2,3]) |
chr(i) | 整数→字符 | chr(65) → 'A' |
ord(c) | 字符→整数 | ord('A') → 65 |
hex(n) | 整数→十六进制 | hex(255) → '0xff' |
oct(n) | 整数→八进制 | oct(8) → '0o10' |
bin(n) | 整数→二进制 | bin(10) → '0b1010' |
ascii(x) | ASCII 表示 | ascii("你好") → "'\\u4f60\\u597d'" |
repr(x) | 开发者表示 | repr("hi") → "'hi'" |
format(x, spec) | 格式化 | format(3.14, ".1f") → "3.1" |
memoryview(x) | 内存视图 | memoryview(b"abc") |
数学运算
| 函数 | 说明 | 示例 |
|---|
abs(x) | 绝对值 | abs(-5) → 5 |
round(x, n) | 四舍五入 | round(3.14, 1) → 3.1 |
pow(x, y, z) | 幂运算 | pow(2, 10) → 1024 |
divmod(a, b) | 商和余数 | divmod(17, 5) → (3, 2) |
max(...) | 最大值 | max(1, 3, 2) → 3 |
min(...) | 最小值 | min(1, 3, 2) → 1 |
sum(iter) | 求和 | sum([1,2,3]) → 6 |
迭代与序列
| 函数 | 说明 | 示例 |
|---|
len(x) | 长度 | len([1,2,3]) → 3 |
range(stop) | 范围序列 | list(range(5)) → [0,1,2,3,4] |
enumerate(iter) | 带索引迭代 | list(enumerate("ab")) → [(0,'a'),(1,'b')] |
zip(*iters) | 并行迭代 | list(zip([1,2],[3,4])) → [(1,3),(2,4)] |
map(f, iter) | 映射 | list(map(str, [1,2])) → ['1','2'] |
filter(f, iter) | 过滤 | list(filter(bool, [0,1,2])) → [1,2] |
sorted(iter) | 排序 | sorted([3,1,2]) → [1,2,3] |
reversed(seq) | 反转 | list(reversed([1,2,3])) → [3,2,1] |
iter(x) | 获取迭代器 | iter([1,2,3]) |
next(iter) | 下一个值 | next(iter([1,2])) → 1 |
all(iter) | 全部为真 | all([1, True, "hi"]) → True |
any(iter) | 任一为真 | any([0, False, "hi"]) → True |
slice(stop) | 切片对象 | [1,2,3,4][slice(1,3)] → [2,3] |
对象与类型
| 函数 | 说明 | 示例 |
|---|
type(x) | 获取类型 | type(42) → <class 'int'> |
isinstance(x, t) | 类型检查 | isinstance(42, int) → True |
issubclass(a, b) | 子类检查 | issubclass(bool, int) → True |
id(x) | 对象标识 | id(42) → 内存地址 |
hash(x) | 哈希值 | hash("hello") → 整数 |
callable(x) | 是否可调用 | callable(print) → True |
dir(x) | 属性列表 | dir([]) → ['append', ...] |
vars(x) | 属性字典 | vars(obj) → obj.__dict__ |
getattr(o, n) | 获取属性 | getattr(obj, "name") |
setattr(o, n, v) | 设置属性 | setattr(obj, "name", "Alice") |
delattr(o, n) | 删除属性 | delattr(obj, "name") |
hasattr(o, n) | 有无属性 | hasattr(obj, "name") → True |
property() | 属性描述符 | @property |
classmethod() | 类方法 | @classmethod |
staticmethod() | 静态方法 | @staticmethod |
super() | 父类代理 | super().__init__() |
object() | 基类实例 | 所有类的基类 |
I/O
| 函数 | 说明 | 示例 |
|---|
print(*args) | 打印输出 | print("hello", end="") |
input(prompt) | 读取输入 | name = input("名字: ") |
open(file) | 打开文件 | open("f.txt", "r") |
其他
| 函数 | 说明 | 示例 |
|---|
breakpoint() | 设置断点 | 进入调试器 |
help(x) | 查看帮助 | help(list) |
globals() | 全局变量字典 | |
locals() | 局部变量字典 | |
exec(code) | 执行代码字符串 | exec("x = 1") |
eval(expr) | 求值表达式 | eval("1 + 2") → 3 |
compile(src) | 编译代码 | 编译为代码对象 |
__import__(name) | 导入模块 | 内部用,推荐用 importlib |
aiter(x) | 异步迭代器 | Python 3.10+ |
anext(iter) | 异步下一个值 | Python 3.10+ |