1.实验任务1
task1.py
程序源代码:
1 import random 2 print('用列表存储随机整数:') 3 lst =[random.randint(0,100) for i in range(5)] 4 print(lst) 5 print('\n用集合存储随机整数:') 6 s1 = {random.randint(0,100) for i in range(5)} 7 print(s1) 8 print('\n用集合存储随机整数:') 9 s2 = set() 10 while len(s2)<5: 11 s2.add(random.randint(0,100)) 12 print(s2)
运行程序截图:
关于问题回答:
(1)问题1:(0,100)可以
(2)问题2:一、list(range(5))的范围是[0,4],不包括5
二、list(range(1,5))的范围是[1,4],也不包括5
(3)问题3:不一定,取不到0
(4)问题4:一定
2.实验任务2
task2-1.py
程序源代码:
1 lst =[55,92,88,79,96] 2 i =0 3 while i<len(lst): 4 print(lst[i],end = ' ') 5 i+=1 6 print() 7 for i in range(len(lst)): 8 print(lst[i],end=' ') 9 print() 10 for i in lst: 11 print(i,end=' ') 12 print()
运行程序截图:
task2-2.py
程序源代码:
1 book_info ={'isbn':'978-7-5356-8297-0','书名':'白鲸记','作者':'克里斯多夫·夏布特','译者':'高文婧','出版社':'湖南美术出版社','售价':82} 2 3 for key,value in book_info.items(): 4 print(f'{key}:{value}') 5 print() 6 7 for item in book_info.items(): 8 print(f'{item[0]}:{item[1]}') 9 print() 10 11 for value in book_info.values(): 12 print(value,end=' ') 13 print() 14 15 for key in book_info.keys(): 16 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('{}.'.format(i+1),end='') 10 new_dict = book_infos[i] 11 for value in new_dict.values(): 12 if value ==new_dict['书名']: 13 print('《{}》,'.format(value),end='') 14 else: 15 print(value) 16 i+=1
运行程序截图:
3.实验任务3
task3.py
程序源代码:
1 import string 2 ts = '''The Zen of Python, by Tim Peters 3 4 Beautiful is better than ugly. 5 Explicit is better than implicit. 6 Simple is better than complex. 7 Complex is better than complicated. 8 Flat is better than nested. 9 Sparse is better than dense. 10 Readability counts. 11 Special cases aren't special enough to break the rules. 12 Although practicality beats purity. 13 Errors should never pass silently. 14 Unless explicitly silenced. 15 In the face of ambiguity, refuse the temptation to guess. 16 There should be one-- and preferably only one --obvious way to do it. 17 Although that way may not be obvious at first unless you're Dutch. 18 Now is better than never. 19 Although never is often better than *right* now. 20 If the implementation is hard to explain, it's a bad idea. 21 If the implementation is easy to explain, it may be a good idea. 22 Namespaces are one honking great idea -- let's do more of those! 23 ''' 24 newts = ts.lower() 25 d ={} 26 ls1 = [] 27 ls2 = [] 28 for i in string.ascii_lowercase: 29 n = newts.count(i) 30 ls1.append(i) 31 ls2.append(n) 32 for i in range(1,len(ls1)): 33 for n in range(0,len(ls1)-i): 34 if ls2[n]<ls2[n+1]: 35 ls2[n],ls2[n+1]=ls2[n+1],ls2[n] 36 ls1[n],ls1[n+1]=ls1[n+1],ls1[n] 37 i=0 38 while i<len(ls1): 39 print('{}:{}'.format(ls1[i],ls2[i])) 40 i+=1
运行程序截图:
4.实验任务4
task4.py
运行源代码:
1 d = {8326:'地信类', 2 8329:'计算机类', 3 8330:'气科类', 4 8336:'防灾工程', 5 8345:'海洋科学', 6 8382:'气象工程'} 7 print('专业代号信息'.center(50,'-')) 8 for key in d.keys(): 9 print('{}:{}'.format(key,d[key])) 10 print('学生专业查询'.center(50,'-')) 11 12 while True: 13 xh = str(input('请输入学号:')) 14 if xh =='#': 15 print('查询结束...') 16 break 17 else: 18 if int(xh[4:8]) in d: 19 print('专业是:{}'.format(d[int(xh[4:8])])) 20 elif int(xh[4:8]) not in d: 21 print('不在这些专业中...')
程序运行截图:
5.实验任务5
task5.py
实验源代码:
1 import random 2 re_day = random.randint(1,31) 3 print('猜猜2023年5月哪一天会是你的lucky day 标签:count,product,cart,products,print,实验,id From: https://www.cnblogs.com/nettj666/p/17332808.html