1.实验任务1
task1.py
实验代码:
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,100),不能取到100
2.有序序列的范围是[0,5),不包括5
有序序列的范围是[1,5), 不包括5
3.一定是5
4.不一定是5,len(s2) < 5 有好几个数, 不一定是5
2.实验任务2
task2_1.py
实验代码:
#列表遍历 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 = ' ') print()
实验结果截图:
task2_2.py
实验代码:
#字典遍历 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() #遍历值: 实现方式2 for key in book_info.keys(): print(book_info[key], end = ' ')
实验结果截图:
task2_3.py
实验代码:
book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, {'书名': '局外人', '作者': '阿尔贝.加缪'}, {'书名': '设计中的设计', '作者': '原研哉'}, {'书名': '万历十五年', '作者': '黄仁宇'}, {'书名': '刀锋', '作者': '毛姆'} ] i = 0 while i < len(book_infos): j = book_infos[i] b = str(j['书名']) j['书名'] = b.join('《》') print(str(i+1)+'.'+j['书名']+','+j['作者']) i += 1
实验结果截图:
3.实验任务3
task3.py
实验代码:
text1=''' 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=text1.lower() lst=[text.count(chr(i)) for i in range(97,97+26)] d={} for i in range(26): d.update({chr(i+97):lst[i]}) lst1=list(d.items()) lst2=[(i,j) for j,i in lst1] for i,j in sorted(lst2,reverse=True): print(f'{j}:{i}')
实验结果截图:
4.实验任务4
实验代码:
code_majors={8326:'地信类', 8329:'计算机类', 8330:'气科类', 8336:'防灾工程', 8345:'海洋科学', 8382:'气象工程'} print('专业代号信息'.center(50, '-')) for key,value in code_majors.items(): print(f'{key}:{value}') print('学生专业查询'.center(50, '-')) code=list(code_majors.keys()) number = input('请输入学号:') while number !='#': number_code = int(number[4:8]) if code_majors.get(number_code)==None: print('不在这些专业中...') else: major=code_majors.get(number_code) print('专业是:'+major) number = input('请输入学号:') print('查询结束...')
实验结果截图:
5.实验任务5
实验代码:
import random xO = random.randint(1,31) print('猜猜2023年5月哪一天是你的lucky day 标签:count,pro,cart,products,print,实验,id From: https://www.cnblogs.com/xjw88/p/17332325.html