时间:2个小时左右
代码:100行左右
博客:1
知识点:Python的高级数据类型
# 高级数据类型 list 列表,tuple 元组,dict 字典,set 集合 # 列表 list num_list = [12, 34, 45, 56, 78] print(type(num_list)) print(num_list) print(len(num_list)) print(dir(num_list)) # 对列表进行操作:增删改查 # 查询 print(num_list.index(12)) # 查询某个元素的下标 print(num_list[3]) # 添加 num_list.insert(0, "思瑜") print(num_list) num_list.append(1.79) # 追加 print(num_list) list1 = [88, 99, 78] # extend 列表的合并 num_list.extend(list1) print(num_list) # 修改 num_list[1] = "小明" print(num_list) # 删除 num_list.remove("小明") # remove放的是数 print(num_list) # pop弹出最后一个元素 print(num_list.pop(3)) print(num_list) # count 查看某一元素在列表中出现的次数 print(num_list.count(78)) num1 = [12, 3, 45, 23, 90, 3] print(num1) num1.sort(reverse=False) print(num1) # 遍历 for item in num_list: print(item) # 元组 特点:不能修改 优点:确保数据的安全,mysql数据库读出来的数据都是tuple类型 num_tuple = (12, 13, 45) print(type(num_tuple)) print(len(num_tuple)) print(dir(num_tuple)) print(num_tuple.index(13)) print(num_tuple.count(12)) # 列表和元组之间的转换 # 把元组转换为list list2 = list(num_tuple) print(type(list2)) print(list2) # 再次转换为tuple tup1 = tuple(list2) print(tup1) print(type(list2)) print(list2) # 字典 dict:是以键值对的方式存储数据 dict_info = { "name": "张三", "age": "18", "score": "90", "classes": "python" } print(dict_info) print(type(dict_info)) print(dir(dict_info)) # 增删改查 # 增 dict_info['height'] = 1.75 print(dict_info) # 修改 dict_info["age"] = 20 print(dict_info) # 删除 print(dict_info.pop("age")) # 根据key删除 print(dict_info.popitem()) print(dict_info) print(dict_info.get("age")) print(dict_info.keys()) print(dict_info.values()) # 对字典进行遍历 for key in dict_info.keys(): print(key) print(dict_info[key]) for value in dict_info.values(): # 只取值 print(value) for item in dict_info.items(): print(item)