首页 / 知识库 / 0基础入门-阅读资料 / 0基础-python入门到精通

附录 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+