实验任务一
实验源码
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:random.randint(0,100)生成的随机整数范围是?能否取到100?
答:范围是0~100,其中包括100
问题2:利用list(range(5)),list(range(1,5))生成的有序序列范围是?是否包括5?
答:list(range(5)的有序序列范围是0~4,其中不包括5;list(range(1,5))生成的有序序列范围是1~4,其中不包括5、
问题3:使用line代码生成的集合s1,len(s1)一定是5吗?如果不是请解释原因。
答:不一定是5,若随机生成两个相同的数,但集合中只保留一个。
问题4:使用line12-14 生成的集合s2,len(s2)一定是5嘛?如果不是请解释原因。
答:一定是5
实验任务2-1
实验源码
1st = [55, 92, 88, 79, 96] i = 0 while i < len(1st): print(1st[i], end = ' ') i += 1 print() for i in range(len(1st)): print() for i in 1st: print(i , end =' ') print()
运行测试截图
实验任务2-2
实验源码
book_info = {'isbn': '978-7-5356-8297-0', '书名': '白鲸记', '作者': '克里斯多夫.夏布寺', '译者': '高文婧', '出版社': '湖南美术出版社', '售价': 82
} for key , value in book_info.items(): print(f'{key}: {value}') print() for item in book_info.items(): print(f'{item[0]}: {item[1]}') print() for value in book_info.values(): print(value, end = ' ') print() for key in book_info.keys(): print(book_info[key], end = ' ')
运行测试截图
实验任务2-3
实验源码
1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 2 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 3 {'书名': '设计中的设计', '作者': '原研哉'}, 4 {'书名': '万历十五年', '作者': '黄仁宇'}, 5 {'书名': '刀锋', '作者': '毛姆'} 6 ] 7 n=1 8 for i in book_infos: 9 print(n,'.''《'+i['书名']+'》',',',i['作者']) 10 n=n+1
运行测试截图
实验任务3
实验源码
1 zen = '''The Zen of Python, by Tim Peters 2 Beautiful is better than ugly. 3 Explicit is better than implicit. 4 Simple is better than complex. 5 Complex is better than complicated. 6 Flat is better than nested. 7 Sparse is better than dense. 8 Readability counts. 9 Special cases aren't special enough to break the rules. 10 Although practicality beats purity. 11 Errors should never pass silently. 12 Unless explicitly silenced. 13 In the face of ambiguity, refuse the temptation to guess. 14 There should be one-- and preferably only one --obvious way to do it. 15 Although that way may not be obvious at first unless you're Dutch. 16 Now is better than never. 17 Although never is often better than *right* now. 18 If the implementation is hard to explain, it's a bad idea. 19 If the implementation is easy to explain, it may be a good idea. 20 Namespaces are one honking great idea -- let's do more of those! 21 ''' 22 zen = zen.lower() 23 letter_sum = dict() 24 for i in range(97,97+26): 25 letter = chr(i) 26 sum1 = zen.count(letter) 27 letter_sum[letter]=sum1 28 letter_sum = sorted(letter_sum.items(),key=lambda x:x[1],reverse = True) 29 for key, value in dict(letter_sum).items(): 30 print(f'{key}: {value}')
运行测试截图
实验任务4
实验源码
1 print('-'*22,'专业代码信息','-'*22) 2 code_majors = {'8326':'地信类','8329':'计算机类','8330':'气科类','8336':'防灾工程','8345':'海洋科学','8382':'气象工程'} 3 for key,value in code_majors.items(): 4 print(f'{key}:{value}') 5 print('-'*22,'学生专业查询','-'*22) 6 while True: 7 numbers = input('请输入学号:') 8 if numbers == '#': 9 print('查询结束') 10 break 11 keys = list(code_majors.keys()) 12 if numbers[4:8] in keys: 13 print(f'专业是:{code_majors[numbers[4:8]]}') 14 else:print('不在这些专业里')
运行测试截图
实验任务5
实验源码
1 import random 2 print('猜猜2023年5月哪一天会是你的lucky day',' 标签:语句,count,pro,编程,数据类型,cart,products,print,id From: https://www.cnblogs.com/yongzhulazhen/p/17332829.html