DAY 02 冲击蓝桥杯——Python基础02数据类型
2.1数据类型
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设置特定数据类型
2.1.5练习题
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..net/article/details/120419754?spm=1001.2014.3001.5502
