1.列表 list 符号:[]
1.1 可以存在空列表: a=[]
1.2 列表中可以包括所有数据类型:a=[1,0.33,'hello',[1,2,3],True]
1.3 列表中的元素 根据逗号进行切割:print(len(a)) -->5
1.4 列表中的元素存在索引:从0开始
1.5 获取列表中的单个值:列表[索引值]
print(a[-1]) -->True
print(a[2]) -->hello
1.6列表的切片 同字符串的操作 列表名[索引头:索引尾:步长] 同字符串一样
print(a[::2]) -->[1,'hello',True]
print(a[0:5:2]) -->[1,'hello',True]
print(a[0:99:2]) -->[1,'hello',True] 根据有多少元素进行切片
1.7列表的使用
列表是存放数据的数据类型
存储数据是一个类型的,建议使用列表
1.8列表增加数据: insert append 可以添加任何类型的数据
append 尾部 追加 不可以追加多个数据,每次只能添加一个
a=[1,0.33,'hello',[1,2,3],True]
a.append("小傻子")
print("a列表的值{0}".format(a)) -->a列表的值[1,0.33,'hello',[1,2,3],True,'小傻子']
insert 任何位置追加,需要指定位置---指定元素的索引值位置 从0开始
a=[1,0.33,'hello',[1,2,3],True]
a.insert(2,"john")
print("a列表的值{0}".format(a)) -->a列表的值[1,0.33,'john','hello',[1,2,3],True]
1.9删除元素 pop() 默认删除最后一个元素 列表名.pop
a=[1,0.33,'hello',[1,2,3],True]
a.pop()
print("a列表的值{0}".format(a)) -->a列表的值[1,0.33,'hello',[1,2,3]]
a=[1,0.33,'hello',[1,2,3],True]
a.pop(1) --> 传入索引值就会删除指定索引位置的元素
print("a列表的值{0}".format(a)) -->a列表的值[1,'hello',[1,2,3],True]
a=[1,0.33,'hello',[1,2,3],True]
res=a.pop() -->pop函数 会返回被删除的那个元素 函数return关键字
print("a列表的值{0}".format(res)) -->a列表的True
a=[1,0.33,'hello',[1,2,3],True]
a.remove(0.33) -->指定删除某个值
print("a列表的值{0}".format(a)) -->a列表的值[1,'hello',[1,2,3],True]
1.10 修改元素的值 列表名[索引值]=新值
a=[1,0.33,'hello',[1,2,3],True]
a[2]='no' -->赋值运算
print("a列表的值{0}".format(a)) -->a列表的值[1,0.33,'no',[1,2,3],True]
a=[1,0.33,'hello',[1,2,3],True,(4,5,6)]
a[-1][1]='no' -->不可以如此操作,不能修改元组中的值
a=[1,0.33,'hello',[1,2,3],True,(4,5,6)]
a[-1]='no' -->可以如此操作,可整体修改元组值
print("a列表的值{0}".format(a)) -->-->a列表的值[1,0.33,'hello',[1,2,3],True,'no']
2.元组 tuple 符号 () 可以是空元组
2.1 可以存在空元组: a=()
2.2 元组中可以包括所有数据类型:a=(1,0.33,'hello',[1,2,3],True,(4,5,6))
2.3 元组中的元素 根据逗号进行切割:print(len(a)) -->5
2.4 元组中的元素存在索引:从0开始
2.5 获取元组中的单个值:元组[索引值]
a=(1,0.33,'hello',[1,2,3],True,(4,5,6))
print(a[-2]) -->True
print(a[2]) -->hello
2.6元组的切片 同字符串的操作 元组名[索引头:索引尾:步长] 同字符串一样
print(a[::2]) -->[1,'hello',True]
print(a[0:6:2]) -->[1,'hello',True]
print(a[0::2]) -->[1,'hello',True]
print(a[0:99:2]) -->[1,'hello',True] 根据有多少元素进行切片
2.7元组的使用 操作数据库时,存放条件
元组是存放数据的数据类型
2.8元组不支持任何修改 (增删改)
不可以修改元素,但是有例外,可以修改里面列表的内容【需要判断是什么类型,是否支持修改,(4,5,6)不支持修改,因为是元组】
a=(1,0.33,'hello',[1,2,3],True,(4,5,6))
a[3][-1]="lalala"
print(a) -->(1,0.33,'hello',[1,2,'lalala'],True,(4,5,6))
2.9如果元组中只有一个元素时,必须要加逗号,才能是元组类型
a=([1,2],)
print(type(a)) -->tuple
a=([1,2])
print(type(a)) -->list
3.字典 dict 符号{} 大括号 花括号 无序 key必须是唯一的
字典存储的方式:key:value 键值对
通过key找到对应的值
3.1 可以存在空字典: a={}
3.2 字典:a={key:value,key:value} value可以包含任何类型的元素
3.3 字典中的元素根据逗号进行切割:
a={"class":"python11",
"student":119,
"teacher" :"girl",
"score" : [99,88.8,100.5]}
print(len(a)) -->4
3.4 字典中的元素不存在索引、不可以切片
3.5 获取字典中的单个值:字典[key]
a={"class":"python11",
"student":119,
"teacher" :"girl",
"score" : [99,88.8,100.5]}
print(a["score"])--> [99,88.8,100.5]
print(a["score"][-1])--> 100.5
3.6 字典删除 pop(key) 指明删除value的key
a={"class":"python11",
"student":119,
"teacher" :"girl",
"score" : [99,88.8,100.5]}
a.pop("teather")
print(a) -->{"class":"python11","student":119,"score" : [99,88.8,100.5]}
res=a.pop("teather")
print(res) -->girl
3.7 字典新增 字典名[新key]=value
a={"class":"python11",
"student":119,
"teacher" :"girl",
"score" : [99,88.8,100.5]}
a["name"]="啦啦啦"
print(a) -->{"class":"python11","student":119,"score" : [99,88.8,100.5],"teacher" :"girl",["name"]:'啦啦啦'}
3.7 字典修改 字典名[已存在key]=新value
a={"class":"python11",
"student":119,
"teacher" :"girl",
"score" : [99,88.8,100.5]}
a["student"]="啦啦啦"
print(a) -->{"class":"python11","student":'啦啦啦',"score" : [99,88.8,100.5],"teacher" :"girl",}
3.8 打印字典值
d= {"age":18,"name":"小白"}
print(d.values()) -->18 小白 #打印字典中的value值
print(d.key()) -->age name #打印字典中的key值