实验任务一
实验源码:
1 # 字符串的基础操作 2 3 x = 'nba FIFA' 4 print(x.upper()) 5 print(x.lower()) 6 print(x.swapcase()) 7 print() 8 9 x = 'abc' 10 print(x.center(10, '*')) 11 print(x.ljust(10, '*')) 12 print(x.rjust(10, '*')) 13 print() 14 15 x = '123' 16 print(x.zfill(10)) 17 x = 123 18 print(str(x).zfill(10)) 19 print() 20 21 x = 'phone_number' 22 print(x.isidentifier()) 23 x = '222test' 24 print(x.isidentifier()) 25 print() 26 27 x = ' ' 28 print(x.isspace()) 29 x = '\n' 30 print(x.isspace()) 31 print() 32 33 x = 'python is fun' 34 table = x.maketrans('thon', '1234') 35 print(x.translate(table))task1
实验截图:
实验任务二
实验源码:
1 # 基础练习: 列表、格式化、类型转换 2 3 x = [5,11,9,7,42] 4 5 print('整数输出1: ', end = '') 6 i = 0 7 while i < len(x): 8 print(x[i], end = ' ') 9 i +=1 10 11 print('\n整数输出2:', end = '') 12 i = 0 13 while i < len(x): 14 print(f'{x[i]:02d}', end = '-') 15 i +=1 16 17 print('\n整数输出3: ', end = '') 18 i = 0 19 while i< len(x) - 1: 20 print(f'{x[i]:02d}', end = '-') 21 i +=1 22 print(f'{x[i]:02d}') 23 24 print('\n字符输出1: ', end = '') 25 y1 = [] 26 i = 0 27 while i < len(x): 28 y1.append(str(x[i])) 29 i += 1 30 print('-'.join(y1)) 31 32 print('字符输出2: ', end = '') 33 y2 = [] 34 i = 0 35 while i < len(x): 36 y2.append(str(x[i]).zfill(2)) 37 i += 1 38 print('-'.join(y2))task2_1
实验截图:
实验源码:
1 # 基础练习: 列表、格式化、类型转换 2 3 x = [1,9,8,4,2,0,49] 4 5 print('整数输出1: ', end = '') 6 i = 0 7 while i < len(x): 8 print(x[i], end = ' ') 9 i +=1 10 11 print('\n整数输出2:', end = '') 12 i = 0 13 while i < len(x): 14 print(f'{x[i]:02d}', end = '-') 15 i +=1 16 17 print('\n整数输出3: ', end = '') 18 i = 0 19 while i< len(x) - 1: 20 print(f'{x[i]:02d}', end = '-') 21 i +=1 22 print(f'{x[i]:02d}') 23 24 print('\n字符输出1: ', end = '') 25 y1 = [] 26 i = 0 27 while i < len(x): 28 y1.append(str(x[i])) 29 i += 1 30 print('-'.join(y1)) 31 32 print('字符输出2: ', end = '') 33 y2 = [] 34 i = 0 35 while i < len(x): 36 y2.append(str(x[i]).zfill(2)) 37 i += 1 38 print('-'.join(y2))task2_2
实验截图:
实验任务三
实验源码:
1 # 把姓名转换成大写,遍历分行输出 2 3 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 4 'cocteau twins'] 5 # 方法1 6 i = 0 7 while i < len(name_list): 8 print(name_list[i].title()) 9 i += 1 10 11 print() 12 13 # 方法2 14 t = [] 15 m = 0 16 while m < len(name_list): 17 t.append(name_list[m].title()) 18 m += 1 19 20 print('\n'.join(t))task3
实验截图:
实验任务四
实验源码:
1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 2 'cocteau twins'] 3 name_list.sort() 4 i = 1 5 while i <= len(name_list): 6 print(f'{i}.', end = '') 7 print(name_list[i-1].title()) 8 i += 1task4
实验截图:
实验任务五
实验源码:
1 text = '''The Zen of Python, by Tim Peters 2 3 Beautiful is better than ugly. 4 Explicit is better than implicit. 5 Simple is better than complex. 6 Complex is better than complicated. 7 Flat is better than nested. 8 Sparse is better than dense. 9 Readability counts. 10 Special cases aren't special enough to break the rules. 11 Although practicality beats purity. 12 Errors should never pass silently. 13 Unless explicitly silenced. 14 In the face of ambiguity, refuse the temptation to guess. 15 There should be one-- and preferably only one --obvious way to do it. 16 Although that way may not be obvious at first unless you're Dutch. 17 Now is better than never. 18 Although never is often better than *right* now. 19 If the implementation is hard to explain, it's a bad idea. 20 If the implementation is easy to explain, it may be a good idea. 21 Namespaces are one honking great idea -- let's do more of those!''' 22 23 x = len(text.split('\n')) 24 y = len(text.split()) 25 z = len(text) 26 m = text.count(' ') 27 print(f'行数: {x}') 28 print(f'单词数: {y}') 29 print(f'字符数: {z}') 30 print(f'空格数: {m}')task5
实验截图:
实验任务六
实验源码:
1 book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'], 2 ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'], 3 ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'], 4 ['来自民间的叛逆', '袁越', '','新星出版社'], 5 ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], 6 ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'], 7 ['小行星掉在下午','沈大成', '', '广西师范大学出版社']] 8 title = '图书信息' 9 print(f'{title:*^40}') 10 i = 0 11 while i < len(book_list): 12 print(f'{i+1},', '|'.join(book_list[i])) 13 i += 1task_6
实验截图:
实验任务七
实验源码:
1 ''' 2 某.csv格式数据文件内数据如下: 3 99 81 75 4 30 42 90 87 5 69 50 96 77 89, 93 6 82, 99, 78, 100 7 ''' 8 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] 9 i = 0 10 n = 0 11 z = 0 12 while i < len(data): 13 list = data[i].split( ) 14 m = 0 15 while m < len(list): 16 n = int(list[m]) 17 sum = n + z 18 z = sum 19 m += 1 20 i += 1 21 22 print(f'{(sum/17):.2f}')task_7
实验截图:
实验任务八
实验源码:
1 words_sensitive_list = ['张三', 'V字仇杀队', '杀'] 2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。', 3 '电影<V字仇杀队>从豆瓣下架了', 4 '娱乐至死'] 5 for x in comments_list: 6 for y in words_sensitive_list: 7 if y in x: 8 x = x.replace(y, '*'*len(y)) 9 print(x)task_8
实验截图:
实验任务九
实验源码:
1 """ 2 家用电器销售系统 3 v1.1 4 """ 5 6 #欢迎信息 7 print('欢迎使用家用电器销售系统!') 8 #产品信息列表 9 print('产品和价格信息如下:') 10 print('********************************************※※***********') 11 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量') 12 print('---------------------------------------------------------------------') 13 print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%-10.2f'%5999.00,'%10d'%20) 14 print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%-10.2f'%6998.00,'%10d'%15) 15 print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%-10.2f'%1999.00,'%10d'%10) 16 print('%-10s'%'0004','%-10s'%'空调','%-10s'%'格力','%-10.2f'%3900.00,'%10d'%0) 17 print('%-10s'%'0005','%-10s'%'热水器','%-10s'%'美的','%-10.2f'%688.00,'%10d'%30) 18 print('%-10s'%'0006','%-10s'%'笔记本','%-10s'%'联想','%-10.2f'%5699.00,'%10d'%10) 19 print('%-10s'%'0007','%-10s'%'微波炉','%-10s'%'苏泊尔','%-10.2f'%480.00,'%10d'%33) 20 print('%-10s'%'0008','%-10s'%'投影仪','%-10s'%'松下','%-10.2f'%1250.00,'%10d'%12) 21 print('%-10s'%'0009','%-10s'%'吸尘器','%-10s'%'飞利浦','%-10.2f'%999.00,'%10d'%9) 22 print('-----------------------------------------------------------------------') 23 24 #商品数据 25 product=[ 26 ['0001','电视机','海尔',5999.00,20], 27 ['0002','冰箱','西门子',6998.00,15], 28 ['0003','洗衣机','小天鹅',1999.00,10], 29 ['0004','空调','格力',3900.00,0], 30 ['0005','热水器','格力',688.00,30], 31 ['0006','笔记本','联想',5699.00,10], 32 ['0007','微波炉','苏泊尔',480.00,33], 33 ['0008','投影仪','松下',1250.00,12], 34 ['0009','吸尘器','飞利浦',999.00,9], 35 ] 36 37 38 #用户输入信息 39 product_id=input('请输入您要购买的产品编号:') 40 count=int(input('请输入您要购买的产品数量:')) 41 42 #获取编号对应商品的信息 43 product_index=len(product_id)-1 44 product=product[product_index] 45 46 #计算金额 47 print('购买成功,您需要支付',product[3] * count,'元') 48 #退出系统 49 print("谢谢您的光临,下次再见!")task9_1
实验截图:
实验源码:
1 """ 2 家用电器销售系统 3 v1.1 4 """ 5 6 #欢迎信息 7 print('欢迎使用家用电器销售系统!') 8 9 #商品数据 10 product=[ 11 ['0001','电视机','海尔',5999.00,20], 12 ['0002','冰箱','西门子',6998.00,15], 13 ['0003','洗衣机','小天鹅',1999.00,10], 14 ['0004','空调','格力',3900.00,0], 15 ['0005','热水器','格力',688.00,30], 16 ['0006','笔记本','联想',5699.00,10], 17 ['0007','微波炉','苏泊尔',480.00,33], 18 ['0008','投影仪','松下',1250.00,12], 19 ['0009','吸尘器','飞利浦',999.00,9], 20 ] 21 22 #产品信息列表 23 print('产品和价格信息如下:') 24 print('*************************************************************************') 25 print('{:10s}{:10s}{:10s}{:10s}{:10s}'.format('编号','名称','品牌','价格','库存数量')) 26 print('--------------------------------------------------------------------------') 27 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0001','电视机','海尔',5999.00,'20')) 28 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0002','冰箱','西门子',6998.00,'15')) 29 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0003','洗衣机','小天鹅',1999.00,'10')) 30 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0004','空调','格力',3900.00,'0')) 31 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0005','热水器','美的',688.00,'30')) 32 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0006','笔记本','联想',5699.00,'10')) 33 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0007','微波炉','苏泊尔',480.00,'33')) 34 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0008','投影仪','松下',1250.00,'12')) 35 print('{:10s}{:10s}{:10s}{:<10.2f}{:10s}'.format('0009','吸尘器','飞利浦',999.00,'9')) 36 print('--------------------------------------------------------------------------') 37 38 #用户输入信息 39 product_id=input('请输入您要购买的产品编号:') 40 count=int(input('请输入您要购买的产品数量:')) 41 42 #获取编号对应商品的信息 43 product_index=len(product_id)-1 44 product=product[product_index] 45 46 #计算金额 47 print('购买成功,您需要支付',product[3] * count,'元') 48 #退出系统 49 print("谢谢您的光临,下次再见!")task9_2
实验截图:
标签:%-,10,len,实验,print,10s From: https://www.cnblogs.com/qjx666/p/17243218.html