task1
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)

task2.1
lst=[55,92,88,79,96] i=0 while i < len(lst): print(lst[i],end=' ') i+=1 print() for i in range(len(lst)): print(lst[i],end=' ') print() for i in lst: print(i,end=' ') print()

task2.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=' ')
for key in book_info.keys():
print(book_info[key],end=' ')

task2.3
book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
{'书名': '局外人', '作者': '阿尔贝.加缪'},
{'书名': '设计中的设计', '作者': '原研哉'},
{'书名': '万历十五年', '作者': '黄仁宇'},
{'书名': '刀锋', '作者': '毛姆'}
]
i=0
while i < len(book_infos):
d=book_infos[i]
for key,value in d.items():
if key == '书名':
print(f'{i+1}.《{d[key]}》,',end='')
if key == '作者':
print(f'{d[key]}')
i+=1

task3
x="""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! """ x.upper() alpha=list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') d = dict.fromkeys(alpha, 0) for i in x.upper(): for key in d.keys(): if i==key: d[key]+=1 for key,value in d.items(): print(f'{key}:{value}')

task4