list01 = ["1","2","3","3","2","6"] # del 是从内存中删除 del list01[1] print(list01) # 统计长度 print(len(list01)) # 统计出现几次 count = list01.count("3") print(count) # remove 列表中第一个出现的 list01.remove("3") print(list01) # 排序 # 升序 list01.sort() print(list01) # 降序 list01.sort(reverse=1) print(list01) # 反转 list01.reverse() print(list01)
list02 =["2","3","4"] for i in list02: print(i)
2 3 4
tuple =("woai",1,0.99) print(type(tuple))
# python解释器,直接认为是数据类型 # <class 'int'> t=(2) print(type(t)) # 要定义一个只包含一个元素的元组 # 要跟上逗号 # <class 'tuple'> t1=(2,) print(type(t1))
t = ("where are you now",1,2,2) # 取值 print(t[0]) # where are you now # 取索引 print(t.index("where are you now")) # 0 # 统计计数 print(t.count(2)) # 2 # 统计元组中元素的个数 print(len(t)) # 4
tuple1 = ("aaa",21,188) print("%s 年龄是%d的身高是%d" % tuple1) tuple1str = "%s 年龄是 %d的身高是%d"%tuple1 print(tuple1str)
# <class 'tuple'> t = (1,23,34) print(type(t)) # <class 'list'> list(t) print(type(list(t)))
{x,y,z}
dict = {"name":"gggg", "age":20, "height":190 } print(dict) # {'name': 'gggg', 'age': 20, 'height': 190} # 打印顺序可能和字典的顺序不一致
dict = {"name":"gggg", "age":20, "height":190 } print(dict) print("取值:",end="") print(dict["name"]) print("增加、修改:",end="") dict["weight"]=200 # key不存在,新增键值对 dict["name"]="ggg" # key存在,修改为小小明 print(dict) print("删除:",end="") dict.pop("name") print(dict)
{'name': 'gggg', 'age': 20, 'height': 190} 取值:gggg 增加、修改:{'name': 'ggg', 'age': 20, 'height': 190, 'weight': 200} 删除:{'age': 20, 'height': 190, 'weight': 200}
dict = {"name":"gggg", "age":20, "height":190 } # 键值对的数量 print("键值对的数量") print(len(dict)) # 合并字典 # {'name': 'gggg', 'age': 20, 'height': 190, 'perfer': 111} temp_dict = { "perfer":111 } dict.update(temp_dict) print(dict) # 清空字典 dict.clear() print(dict)
for i in dict: print("%s-%s"%(i,dict[i]))
字典嵌在 列表中