Python 是一种动态类型语言,其基本数据类型包括以下核心类别:
1. 数值类型
- 整数 (
int)
表示任意大小的整数,例如:$42$, $-3$, $10^{100}$ - 浮点数 (
float)
表示带小数点的数,例如:$3.14$, $-0.001$, $2.0 \times 10^8$ - 复数 (
complex)
形式为 $a + bj$,例如:$3+4j$, $1j$
2. 布尔类型 (bool)
- 仅有两个值:
True(逻辑真)和False(逻辑假) - 常用于条件判断,例如:$x > 0$ 返回布尔值
3. 序列类型
- 字符串 (
str)
不可变的字符序列,例如:"Hello",'Python' - 列表 (
list)
可变的元素序列,例如:[1, 2, 3],['a', True, 3.14] - 元组 (
tuple)
不可变的元素序列,例如:(1, 'apple'),(True,)
4. 映射类型
- 字典 (
dict)
键值对集合,例如:{'name': 'Alice', 'age': 30}
5. 集合类型
- 集合 (
set)
无序不重复元素集,例如:{1, 2, 3}
6. 特殊类型
- 空值 (
NoneType)
唯一值None,表示空或无 - 二进制类型
bytes(不可变字节序列) 和bytearray(可变字节序列)
类型检查示例
print(type(10)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type(True)) # <class 'bool'>
print(type("Hello")) # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>
print(type({'a': 1})) # <class 'dict'>
print(type({1, 2})) # <class 'set'>
print(type(None)) # <class 'NoneType'>
特点总结
- 动态类型:变量类型在运行时确定
- 强类型:不支持隐式类型转换(如
1 + "2"会报错) - 对象机制:所有类型均为对象,可调用方法(如
"abc".upper()

被折叠的 条评论
为什么被折叠?



