2.1数据类型
2.1.1 内置数据类型2.1.2 获取数据类型2.1.3 获取数据类型2.1.4设置特定数据类型2.1.5练习题
2.1.1 内置数据类型文本类型: str数字类型: int, float, complex序列类型: list, tuple, range映射类型: dict套装类型: set, frozenset布尔类型: bool二进制类型:bytes, bytearray, memoryview 2.1.2 获取数据类型
type()用来获取数据类型
x=2 print(type(x))2.1.3 获取数据类型
在 Python 中,数据类型是在为变量赋值时设置的。
(1)str字符串:
x='hgzsq' print(x) print(type(x))
(2)int整形:
x=3 print(x) print(type(x))
(3)float浮点类型:
x=3.2 print(x) print(type(x))
(4)complex复数类型:
x=3+2j print(x) print(type(x))
(5)list列表类型:
x4 = ["apple", "banana", "cherry"] print(x4) print(type(x4))
(6)tuple元组类型:
x5 = ("apple", "banana", "cherry")
print(x5)
print(type(x5))
(7)range范围类型:
x = range(6)
(8)dict字典类型
x = {"name" : "John", "age" : 36}
(9)set集合类型:
x = {"apple", "banana", "cherry"}
(10)bool布尔类型:
x = True
(11)不常用byte字节类型:
x = b"Hello" print(type(x))
(12)不常用bytearray字节数组类型:
x = bytearray(5) print(type(x))
(13)更有冷门到爆的memoryview内存视图类型
x = memoryview(bytes(5)) print(type(x))2.1.4设置特定数据类型
| 强调类型 | 强调表达式 |
| 整型 | int(x) |
| 浮点型 | float(x) |
| 复数 | complex(x) |
| 列表 | list(x) |
| 元组 | tuple(x) |
| 范围 | range(numbers) |
| 字典 | set(x) |
| 集合 | dict(x) |
| 冻结集 | frozenset(x) |
| 布尔类型 | bool(x) |
| 字节类型 | bytes(numbers) |
| 字节组类型 | bytearray(numbers) |
| 内存视图类型 | memoryview(bytes(numbers) |
Q1:
x = 5 print(type(x))
int类型
Q2:
x = 5 x = "Hello World" print(type(x))
str类型
Q3:
x = 20.5 print(type(x))
float类型
Q4:
x = ["apple", "banana", "cherry"] print(type(x))
list类型
Q5:
x = {"name" : "John", "age" : 36}
print(type(x))
字典类型
Q6:
x = True print(type(x))
Bool类型
参考文档
https://chuanchuan.blog.csdn.net/article/details/120419754?spm=1001.2014.3001.5502



