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?
可以
问题2:利用 list(range(5)) 生成的有序序列范围是?是否包括5?
包括
问题3:使用line8代码生成的集合s1,len(s1)一定是5吗?如果不是,请解释原因。
一定
问题4:使用line12-14生成的集合s2,len(s2)一定是5吗?如果不是,请解释原因。
一定
实验任务二
task2_1程序源代码
#列表遍历 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程序源代码
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程序源代码
book_info = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, {'书名': '局外人', '作者': '阿尔贝.加缪'}, {'书名': '设计中的设计', '作者': '原研哉'}, {'书名': '万历十五年', '作者': '黄仁宇'}, {'书名': '刀锋', '作者': '毛姆'} ] k = [] a = [] for i in book_info: for key in i.keys(): k.append(i[key]) break for i in book_info: for item in i.items(): a.append(item[1]) for i in k: if i in a: a.remove(i) for i in range(1,6): print(i,end='.') print(f'《{k[i-1]}》',end = ',') print(a[i-1])
运行结果
实验任务三
程序源代码
s = '''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!''' k= [] for i in s: if 65<=ord(i)<=90 or 97<=ord(i)<=122: k.append(i.lower()) else: continue for i in range(97,122): print(f'{chr(i)}:{k.count(chr(i))}')
运行结果
实验任务四
程序源代码
def chack(dic,value): for key in dic: if value in dic[key]: return key break code_majors = {8326:'地信类',8329:'计算机类',8330:'气科类',8336:'防灾工程',8345:'海洋科学',8382:'气象工程'} studentdict = {'海洋科学':[202283450521,202283450540,202283450321], '防灾工程':[202283361234,202283360045,202283360012], '地信类':[202283260001,202283260024,202283260003], '计算机类':[202283290001,202283290024,202283290003], '气科类':[202283300001,202283300024,202283300003], '气象工程':[202283820001,202283820024,202283820003]} x='专业代号信息' y='学生专业查询' print(x.center(50,"-")) for key in code_majors: print(f'{key}:{code_majors[key]}') print(y.center(50,"-")) for i in studentdict: print(i) x = input('请输入学号:') while x != '#': if chack(studentdict,int(x)) == None: print('不在这些专业中...') x = input('请输入学号:') else: print(chack(studentdict,int(x))) x = input('请输入学号:') print('查询结束...')
运行结果
实验任务五
程序源代码
import random lucky = random.randint(1,31) right = False k = 1 print('猜猜2023年5月哪一天会是你的lucky day 标签:语句,count,编程,数据类型,cart,amount,products,print,id From: https://www.cnblogs.com/yang2002/p/17332485.html