元组:类似列表(当成容器)
一、特点
1、定义的符号:()
2、元组中的内容不可修改
3、关键字:tuple
4、元组只要含有一个元素,不需要加“,”,如(1,)
二、元组中的函数
1、index(obj) ----> 下标位置
从元组中找出下标位置,没有报错ValueError
2、count() ----> 个数
三、拆包
1、a,b = (1,2,3):ValueError:too many values to unpack(expected 2)
2、a,b,c = (6,):ValueError:not enough values to unpack(expected 3,got 1)
3、变量个数与元组个数不一致:*表示未知个数0-n
a,b,*c = (1,2,3,4,5) ----> 1,2,[3,4,5]
a,*b = (1,) ----> 1,[]
四、总结
1、符号
+
*
is not
in not in
2、系统函数
max()
min()
sum()
len()
sorted() ----> 排序,返回的结果就是列表
tuple() ----> 元组类型的强制转换
3、元组自带函数
index()
count()
4、拆装包
字典:
对比:
1、增加元素
list.append(element)
dict[key]=value
2、修改
list[index]=newvalue
dict[key]=value
3、查询元素
list[index] ----> element
dict[index] ----> value