数据类型
基本的数据类型——数字
布尔型:
1. bool型只有两个值:True和False
2. 之所以将布尔值归为数字,因为我们习惯用1 表示True,0表示False。
整型
浮点型
复数
基本的数据类型——字符串
字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,' '或'' ''或''' '''中间包含的内容称之为字符串
字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r'l\thf'
基本的数据类型——列表
定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素
常用操作:
1. 索引
1 color = ["red","blue","green","white"] 2 索引: 3 print('color[0]')
2. 切片
1 color = ["red","blue","green","white"] 2 print("color[0:2]") 3 >>>red,blue 4 5 print("color[1:3]") 6 >>>blue,green
倒着切
color = ["red","blue","green","white"]
color[-1:-5:-1]
字符串也可以反转
n = "进击的小吴"
n[::-1]
>>>n = "吴小的击进"
3. 追加
1 color = ["red","blue","green","white"] 2 color.append("black") 3 >>>color = ["red","blue","green","white","black"]
插入
color = ["red","blue","green","white"]
color.insert(2,"pink")
print(color.insert(2,"pink"))
>>>
color = ["red","blue","pink","green","white"]
4. 删除
1 color = ["red","blue","green","white"] 2 color.remove("white") #删除从左到右的第一个值 3 4 color = ["red","blue","green","white"] 5 color.pop()
>>>"white" 6 >>>color = ["red","blue","green"]
color = ["red","blue","green","white"]
print("del color[red]")
>>>color = ["blue","green","white"]
5. 长度
1 len(color) 2 >>>3
6. 包含和循环
>>> 'red' in color True >>> 'pink' in color False #循环:为什么是“i”? >>> for i in color: print(i)
7. 排序
1 a = [2,3,4,9,7,15] 2 a.sort() 3 print(a.sort())
>>>a = [2,3,4,7,9,15]
8. 查询
1 color = ["red","blue","green","white"] 2 color.index("blue") 3 >>>print(color.index("blue")) 4 >>>1
color = ["red","blue","green","white"]
color.count("red")
>>>1