Skip to main content
Documents
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Built-in Types (内置类型)

官方文档:https://docs.python.org/zh-cn/3.12/library/stdtypes.html

Python 的“内置类型”(Built-in Types)指 Python 语言内置、无需导入即可使用的数据类型。它们是整个语言的基础。下面是 最完整、最系统的一份 Python 内置类型总表。

🧩 Python 内置类型总览(官方分类)

Python 官方文档,把内置类型分为 6 大类:

1. None 类型

类型 说明
NoneType 只有一个值:None(表示空、缺失、无返回结果)

使用场景:函数无返回值、默认参数、占位符等。

2. 数值类型(Numeric Types)

类型 说明 示例
int 整数(无限精度) 1, 9999999
float 浮点数(双精度) 1.23, 3e10
complex 复数 3+4j
bool 布尔值(int 的子类) True, False

额外注意:

bool 是 int 的子类:

isinstance(True, int)  # True

3. 序列类型(Sequence Types)

📌 按是否可变分两类

✅ 不可变序列

类型 说明 示例
str 字符串 “hello”
tuple 元组 (1,2,3)
bytes 不可变字节序列 b"abc"

🔧 可变序列

类型 说明 示例
list 列表 [1, 2, 3]
bytearray 可变字节序列 bytearray(b"abc")

所有序列都支持:

  • 索引 x[0]
  • 切片 x[1:5]
  • len(x)
  • 迭代
  • in

4. 集合类型(Set Types)

类型 可变? 示例
set ✅ 可变 {1, 2, 3}
frozenset ❌ 不可变 frozenset([1,2,3])

常用操作:交并差、去重。

5. 映射类型(Mapping Types)

类型 说明 示例
dict 字典(哈希表) {“a”:1, “b”:2}

字典是 Python 最重要的数据结构之一。

6. 上下文管理器(Context Manager)

他们不是“具体类型”,而是实现了:

  • __enter__
  • __exit__

常见内置上下文管理器:

类型 示例
文件对象 with open(...) as f:
锁对象 with threading.Lock():
decimal 环境 with decimal.localcontext():

7. 迭代器与生成器类型(Iterator / Generator)

类型 说明 示例
range 惰性序列 range(10)
enumerate 枚举对象 enumerate(lst)
zip 惰性组合 zip(a,b)
map 惰性映射 map(f, xs)
filter 惰性过滤 filter(f, xs)
generator 生成器 yield 产生的对象

只要对象实现:

  • __iter__
  • __next__

就属于“迭代器类别”。

8. 函数、类、模块等

这些也属于内置类型,只是常被忽略。

类型 说明
function 普通函数
builtin_function_or_method 内置函数,如 len()
method 方法
type 类(类本身也是对象)
module 模块对象
code 函数对应的代码对象
frame 运行帧对象
memoryview 内存视图(零拷贝)

☘️ Python 内置类型一览图(快速记忆)

  1. None
  2. 数值:int, float, complex, bool
  3. 序列:
    • 不可变:str, tuple, bytes
    • 可变:list, bytearray
  4. 集合:set, frozenset
  5. 映射:dict
  6. 惰性:range, map, filter, zip, enumerate, generator
  7. 函数与类:function, type, method
  8. 其他:module, memoryview, file, context manager 等