实验结论
实验任务1,2,3
1 # 基础操作练习: 列表及列表推导式、格式化、类型转换 2 def task1(): 3 x = list(range(10)) 4 5 print('整数输出1: ', end = '') 6 for i in x: 7 print(i, end=' ') 8 9 print('\n整数输出2: ', end = '') 10 for i in x: 11 print(f'{i:02d}', end = '-') # 指定每个整数输出宽度占两列,不足两列,左边补0 12 13 print('\n整数输出3: ', end = '') 14 for i in x[:-1]: 15 print(f'{i:02d}', end = '-') 16 print(f'{x[-1]:02d}') 17 18 print('\n字符输出1: ', end = '') 19 y1 = [str(i) for i in range(10)] # 函数str()用于把其它类型对象转换成字符串对象 20 print('-'.join(y1)) 21 22 print('字符输出2: ', end = '') 23 y2 = [str(i).zfill(2) for i in range(10)] # 方法.zfill()用于对字符串进行格式化, 指定宽度为2列,不足左边补0 24 print('-'.join(y2)) 25 26 # 把姓名转换成大写,遍历输出 27 def task2(): 28 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] 29 # 实现方式1 30 for name in name_list: 31 print(name.title()) 32 print() 33 # 实现方式2 34 name_list_captilize = [name.title() for name in name_list] 35 print('\n'.join(name_list_captilize)) 36 37 def task3(): 38 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 39 'cocteau twins'] 40 name_list=sorted(name_list) 41 i=1 42 for name in name_list : 43 print("%d."%(i),end=" ") 44 print(name.title()) 45 i+=1
实验1,2,3截图
实验任务4
1 def task4(): 2 text ="""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 words=0;i=1 24 while(i<len(text)): 25 if ((text[i-1]<="z" and text[i-1]>="a" or text[i-1]<="Z" and text[i-1]>="A") 26 and not(text[i]<="z" and text[i]>="a" or text[i]<="Z" and text[i]>="A" or text[i]=="'") 27 ): 28 words+=1;i+=1;continue 29 else: 30 i+=1 31 words+=1 #Add the last word 32 print("Linage:%d"%(text.count("\n")+1))#Add the last line 33 print("Words number:%d"%(words)) 34 print(f"Charactor number:{len(text)}") 35 print("Space number:{}".format(text.count(" ")))
实验任务4截图
实验任务5,6,7
def task5(): book_list = [ ['静静的顿河','肖洛霍夫','金人', '人民文学出版社'], ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'], ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'], ['来自民间的叛逆', '袁越', '','新星出版社'], ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'], ['小行星掉在下午','沈大成', '', '广西师范大学出版社'] ] for i in range(len(book_list)): print(f"{i+1}.《{book_list[i][0]}》|{book_list[i][1]}|{book_list[i][3]}") def task6(): ''' 某.csv格式数据文件内数据如下: 99 81 75 30 42 90 87 69 50 96 77 89, 93 82, 99, 78, 100 ''' data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] Sum=0;i=0 for team in data: numbers=team.split() for number in numbers: Sum+=eval(number);i+=1 average=Sum/i print("%.2f"%(average)) def task7(): words_sensitive_list = ['张三', 'V字仇杀队', '杀'] comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。', '电影<V字仇杀队>从豆瓣下架了', '娱乐至死'] for comments in comments_list: for words in words_sensitive_list: comments=comments.replace(words,"*"*len(words)) print(comments)
实验任务5,6,7截图
实验总结:
Python丰富的函数库确实便于解决问题,但过多的函数代替了具体算法的训练必将导致对底层逻辑的疏忽及基础编程能力的缺失。实验应更加重视底层逻辑和基础算法方面的训练。