task1
程序源码:
1 import random 2 3 print('用列表储存随机整数:') 4 lst = [random.randint(0,100) for i in range(5)] 5 print(lst) 6 7 print('\n用集合存储随机整数:') 8 s1 = {random.randint(0,100)for i in range(5)} 9 print(s1) 10 11 print('\n用集合存储随机整数:') 12 s2 = set() 13 while len(s2) < 5: 14 s2.add(random.randint(0,100)) 15 print(s2)
运行截图:
问题1: random.randint(0,100) 生成的随机整数范围是?能否取到100? [0,100],能取到100。
问题2:利用 list(range(5)) 生成的有序序列范围是?是否包括5? [0,1,2,3,4],不包括5。
利用 list(range(1,5)) 生成的有序序列范围是?是否包括5? [1,2,3,4],不包括5。
问题3:使用line8代码生成的集合s1,len(s1)一定是5吗?如果不是,请解释原因。 len(s1)不一定是5,如果生成的随机数重复了,集合会去掉重复的随机数,len(s1)就会小于5。
问题4:使用line12-14生成的集合s2,len(s2)一定是5吗?如果不是,请解释原因。 len(s1)一定是5。
task2_1
程序源码:
1 #列表遍历 2 lst = [55,92,88,79,96] 3 4 #遍历方式1: 使用while + 索引 5 i = 0 6 while i < len(lst): 7 print(lst[i],end = ' ') 8 i += 1 9 10 print() 11 12 #遍历方式2:使用for + 索引 13 for i in range(len(lst)): 14 print(lst[i],end = ' ') 15 print() 16 17 #遍历方式3:使用for...in 18 for i in lst: 19 print(i,end = ' ') 20 print()
运行截图:
task2_2
程序源码:
1 #字典遍历 2 book_info = {'isbn': '978-7-5356-8297-0', 3 '书名': '白鲸记', 4 '作者': '克里斯多夫.夏布特', 5 '译者': '高文婧', 6 '出版社': '湖南美术出版社', 7 '售价': 82 8 } 9 #遍历key-value对: 实现方式1 10 for key, value in book_info.items(): 11 print(f'{key}: {value}') 12 print() 13 14 #遍历key-value对: 实现方式2 15 for item in book_info.items(): 16 print(f'{item[0]}: {item[1]}') 17 print() 18 19 #遍历值:实现方式1 20 for value in book_info.values(): 21 print(value, end = ' ') 22 print() 23 24 #遍历值:实现方式2 25 for key in book_info.keys(): 26 print(book_info[key], end = ' ')
运行截图:
task2_3
程序源码:
1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 2 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 3 {'书名': '设计中的设计', '作者': '原研哉'}, 4 {'书名': '万历十五年', '作者': '黄仁宇'}, 5 {'书名': '刀锋', '作者': '毛姆'} 6 ] 7 for i in range(len(book_infos)): 8 l=[v for v in book_infos[i].values()] 9 print(f'{i+1}.《{l[0]}》,{l[1]}')
程序截图:
task_3
程序源码:
1 text = '''The Zen of Python, by Tim Peters 2 3 Beautiful is better than ugly. 4 Explicit is better than implicit. 5 Simple is better than complex. 6 Complex is better than complicated. 7 Flat is better than nested. 8 Sparse is better than dense. 9 Readability counts. 10 Special cases aren't special enough to break the rules. 11 Although practicality beats purity. 12 Errors should never pass silently. 13 Unless explicitly silenced. 14 In the face of ambiguity, refuse the temptation to guess. 15 There should be one-- and preferably only one --obvious way to do it. 16 Although that way may not be obvious at first unless you're Dutch. 17 Now is better than never. 18 Although never is often better than *right* now. 19 If the implementation is hard to explain, it's a bad idea. 20 If the implementation is easy to explain, it may be a good idea. 21 Namespaces are one honking great idea -- let's do more of those!''' 22 23 l = [text.lower().count(chr(i)) for i in range(97,97+26)] 24 d = {} 25 for j in range(26): 26 d.update({chr(j+97):l[j]}) 27 28 l1 = list(d.items()) 29 l2 = [(v,k) for k,v in l1] 30 for v,k in sorted(l2,reverse=True): 31 print(f'{k}:{v}')
运行截图:
task_4
程序源码:
1 code_majors = {8326:'地信类', 2 8329:'计算机类', 3 8330:'气科类', 4 8336:'防灾工程', 5 8345:'海洋科学', 6 8382:'气象工程' 7 } 8 print(f'{"专业代号信息":-^50s}') 9 for k,v in code_majors.items(): 10 print(f'{k}:{v}') 11 12 print(f'{"学生专业查询":-^50s}') 13 n = input('请输入学号:') 14 while n != '#': 15 if int(n[4:8]) in code_majors: 16 print(f'专业是:{code_majors.get(int(n[4:8]))}') 17 else: 18 print('不在这些专业中...') 19 n = input('请输入学号:') 20 print('查询结束...')
运行截图:
task_5
程序源码:
1 import random 2 a = random.randint(1,31) 3 print('猜猜2023年5月哪天是你的lucky day: 标签:语句,count,product,编程,数据类型,cart,products,print,id From: https://www.cnblogs.com/p-s-y-0309/p/17332611.html