1。实验任务1
task1.py
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)
2.实验任务2
task2_1.py
1 # 列表遍历 2 lst = [55,92,88,79,96] 3 #遍历方式1:使用while + 索引 4 i = 0 5 while i < len(lst): 6 print(lst[i],end = ' ') 7 i+=1 8 9 print() 10 11 #遍历方式2:使用for + 索引 12 for i in range(len(lst)): 13 print(lst[i],end = ' ') 14 print() 15 16 #遍历方式3:使用for...in 17 for i in lst: 18 print(i,end = ' ') 19 print()
task2_2.py
1 # 字典遍历 2 book_info = {'isbn': '978-7-5356-8297-0', 3 '书名': '白鲸记', 4 '作者': '克里斯多夫.夏布特', 5 '译者': '高文婧', 6 '出版社': '湖南美术出版社', 7 '售价': 82 8 } 9 10 # 遍历key-value对: 实现方式1 11 for key,value in book_info.items(): 12 print(f'{key}:{value}') 13 print() 14 15 #遍历key-value对: 实现方式2 16 for item in book_info.items(): 17 print(f'{item[0]}:{item[1]}') 18 print() 19 20 #遍历值: 实现方式1 21 for value in book_info.values(): 22 print(value, end = ' ') 23 print() 24 25 #遍历值:实现方式2 26 for key in book_info.keys(): 27 print(book_info[key],end = ' ')
task2_3.py
1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 2 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 3 {'书名': '设计中的设计', '作者': '原研哉'}, 4 {'书名': '万历十五年', '作者': '黄仁宇'}, 5 {'书名': '刀锋', '作者': '毛姆'} 6 ] 7 i = 0 8 while i < len(book_infos): 9 print(i+1,end ='. ') 10 x = [] 11 for value in book_infos[i].values(): 12 x.append(value) 13 print(f'《{x[0]}》, {x[1]}') 14 i+=1
3.实验任务3
task3.py
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 text1 = text.lower() 24 alpha_dict = {} 25 for i in range(97,123): 26 alpha = chr(i) 27 number = text1.count(alpha) 28 alpha_dict[alpha] = number 29 ans = sorted(alpha_dict.items(),key=lambda x:x[1],reverse = True) 30 for j in ans: 31 print(f'{j[0]}:{j[1]}')
4.实验任务4
task4.py
1 code_majors = {'8326': '地信类', 2 '8329': '计算机类', 3 '8330': '气科类', 4 '8336': '防灾工程', 5 '8345': '海洋科学', 6 '8382': '气象工程' 7 } 8 9 print('{:-^50}'.format('专业代号信息')) 10 for key,value in code_majors.items(): 11 print(f'{key}:{value}') 12 print('{:-^50}'.format('学生专业查询')) 13 stu_number = input('请输入学号:') 14 15 while True: 16 if stu_number[4:8] in code_majors: 17 x = code_majors[stu_number[4:8]] 18 print(f'专业是:{x}' ) 19 stu_number = input('请输入学号:') 20 elif stu_number == '#': 21 print('查询结束...') 22 break 23 else: 24 print('不在这些专业中...') 25 stu_number = input('请输入学号:')
5.实验任务5
task5.py
1 import random 2 lucky_day = random.randint(1,31) 3 print('猜猜2023年5月哪一天会是你的lucky day' + chr(0x1f600)) 4 number = int(input('你有三次机会,猜吧(1~31):')) 5 i = 1 6 while True: 7 if number >31 or number <1: 8 print('地球上没有这一天啦,你是外星人吗') 9 i+=1 10 if i == 4: 11 print('哇哦,次数用光啦.') 12 print(f'偷偷告诉你,5月你的lucky day是 {lucky_day} 号.good luck' + chr(0x1f601) ) 13 break 14 number = int(input('再猜(1~31):')) 15 16 17 elif number < lucky_day: 18 print('猜早啦,你的lucky day还没到呢') 19 i+=1 20 if i == 4: 21 print('哇哦,次数用光啦.') 22 print(f'偷偷告诉你,5月你的lucky day是 {lucky_day} 号.good luck' + chr(0x1f601) ) 23 break 24 number = int(input('再猜(1~31):')) 25 26 27 elif number > lucky_day: 28 print('猜晚啦,你的lucky day已经过去啦') 29 i+=1 30 if i == 4: 31 print('哇哦,次数用光啦.') 32 print(f'偷偷告诉你,5月你的lucky day是 {lucky_day} 号.good luck' + chr(0x1f601) ) 33 break 34 number = int(input('再猜(1~31):')) 35 36 37 elif number == lucky_day: 38 print('哇,猜中了' + chr(0x1f602)) 39 break
标签:语句,遍历,编程,数据类型,number,value,book,key,print From: https://www.cnblogs.com/wang2003chengxu/p/17332813.html