内置数据类型
变量可以存储不同类型的数据,并且不同类型可以执行不同的操作。
在这些类别中,Python 默认拥有以下内置数据类型:
文本类型: | str |
数值类型: | int, float, complex |
序列类型: | list, tuple, range |
映射类型: | dict |
集合类型: | set, frozenset |
布尔类型: | bool |
二进制类型: | bytes, bytearray, memoryview |
type() 函数
获取数据类型。
设置数据类型
x = "Hello World" | str | 字符串 |
x = 29 | int | 整数(正整数、负整数、0) |
x = 29.5 | float | 浮点型 |
x = 1j | complex | 复数 |
x = ["apple", "banana", "cherry"] | list | 列表 |
x = ("apple", "banana", "cherry") | tuple | 元组 |
x = range(6) | range | 范围 |
x = {"name" : "Bill", "age" : 63} | dict | 字典 |
x = {"apple", "banana", "cherry"} | set | 集合 |
x = frozenset({"apple", "banana", "cherry"}) | frozenset | 冻结/不可变集合 |
x = True | bool | 布尔 |
x = b"Hello" | bytes | 字节 |
x = bytearray(5) | bytearray | 字节数组 |
x = memoryview(bytes(5)) | memoryview | 返回给定参数的内存查看对象 |