实验任务1
源代码:
import random print('用列表存储随机整数:') lst = [random.randint(0,100) for i in range (5)] print(lst) print('\n用集合存储随机整数: ') s1 = {random.randint(0,100) for i in range(5)} print(s1) print('\n用集合存储随机整数:') s2 = set() while len(s2) <5: s2.add(random.randint(0,100)) print(s2)
运行结果:
问题1:0-99,可以
问题2:0-4,不包括;1-4,不包括
问题3:不一定,集合会自动去除重复元素
问题4:是
实验任务2
源代码:
#遍历列表 lst = [55,92,88,79,96] #方式1——使用while + 索引 i = 0 while i < len(lst): print(lst[i],end=' ') i+=1 print() #方式2——使用for + 索引 for i in range(len(lst)): print(lst[i],end=' ') print() #方式3——使用for....in for i in lst: print(i,end=' ')
#字典遍历 book_info = {'ISBN':'978-7-5356-8297-0', '书名':'白鲸记', '作者':'克里斯多夫·夏布特', '译者':'高文婧', '出版社':'湖南美术出版社', '售价':82} #遍历key-value对:1 for key,value in book_info.items(): print(f'{key}:{value}') print() #遍历key-value对:2 for item in book_info.items(): print(f'{item[0]}:{item[1]}') print() #遍历值:1 for value in book_info.values(): print(value,end=' ') print(f"\n") #遍历值:2 for key in book_info.keys(): print(book_info[key],end=' ')
book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, {'书名': '局外人', '作者': '阿尔贝.加缪'}, {'书名': '设计中的设计', '作者': '原研哉'}, {'书名': '万历十五年', '作者': '黄仁宇'}, {'书名': '刀锋', '作者': '毛姆'} ] for i in range(5): a=book_infos[i] for info in a.values(): print(info,end=',') print()
实验任务3
源代码:
text = """The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!""" text = text.lower()#转换成小写 c = [chr(n) for n in range(97,123)]#创建字母列表 quantity = {} for i in text: if i not in quantity: quantity[i] = 1 elif i in quantity: quantity[i] += 1 l1 = []#空列表存放文段中不是字母的值 for cha in quantity.keys(): if cha not in c: l1.append(cha) #删除非字母项 for l_1 in l1: del quantity[l_1] #添加没有的字母为:0 for notin in c: if notin in c and notin not in quantity.keys(): quantity[notin] = 0 for zimu,shuliang in quantity.items(): print(f"{zimu}:{shuliang}")
实验任务4
源代码:
code_majors = {8326:'地信类',8329:'计算机类',8330:'气科类',8336:'防灾工程',8345:'海洋科学',8382:'气象工程'} headline_1 = "专业代号信息" print(f"{headline_1:-^50s}") for code,major in code_majors.items(): print(f"{code}: {major}") headline_2 = "学生专业查询" print(f"{headline_2:-^50s}") codes = [int(c) for c in code_majors.keys()] ID = str(input("请输入学号")) while len(ID) == 12: print(ID) ID = int(ID[4:8]) if ID in codes: relate_major = code_majors[ID] print(f"专业是: {relate_major}") ID = str(input("请输入学号")) else: print("不在这些专业中") ID = str(input("请输入学号")) while ID == "#": print("查询结束...") break
实验任务5
源代码:
import random print("猜猜2023年5月份哪天是你的lucky day 标签:count,pro,cart,任务,products,print,实验,id From: https://www.cnblogs.com/minkongchan/p/17353692.html