实验任务1
task1
实验源码
1 #字符串的基础操作 2 #课堂上没有演示的一些方法 3 4 x = 'nba FIFA' 5 print(x.upper()) 6 print(x.lower()) 7 print(x.swapcase()) #字符串大小写翻转 8 print() 9 10 x = 'abc' 11 print(x.center(10, '*')) #字符串居中,宽度10列,不足左右补*号 12 print(x.ljust(10, '*')) 13 print(x.rjust(10, '*')) 14 print() 15 16 x = '123' 17 print(x.zfill(10)) 18 x = 123 19 print(str(x).zfill(10)) #把int类型转换成字符串类型后,对字符串对象使用str.zfill()方法 20 print() 21 x = 'phone_number' 22 print(x.isidentifier()) #判断字符串是否是python合法标识符 23 x = '222test' 24 print(x.isidentifier()) 25 print() 26 27 x = ' ' 28 print(x.isspace()) #判断字符串是否是空白(包括空格,回车,Tab键) 29 x = '\n' 30 print(x.isspace()) 31 print() 32 33 x = 'python is fun' 34 table = x.maketrans('thon', '1234') #为字符串对象x创建一个字符映射表,字符thon分别映射到字符1234 35 print(x.translate(table)) #根据字符映射表table对字符串对象x中的字符进行转换
运行测试截图
实验任务2
task2
实验源码
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 12 print('\n整数输出2:', end = '') 13 i = 0 14 while i < len(x) - 1: 15 print(f'{x[i]:02d}', end = '-') 16 i += 1 17 print(f'{x[-1]:02d}') 18 19 20 print('\n字符输出1: ', end = '') 21 y1 = [] 22 i = 0 23 while i < len(x): 24 y1.append(str(x[i])) # 函数str()用于把其它类型对象转换成字符串对象 25 i += 1 26 print('-'.join(y1)) 27 28 print('字符输出2: ', end = '') 29 y2 = [] 30 i = 0 31 while i < len(x): 32 y2.append( str(x[i]).zfill(2) ) # x[i]是int类型对象,使用str()转换成字符串对象后,处理,然后加入到列表y中 33 i += 1 34 print('-'.join(y2))
运行运行测试
实验任务3
task3
实验源码
1 # 把姓名转换成大写,遍历分行输出 2 3 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteau twins'] 4 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 i = 0 16 while i < len(name_list): 17 t.append(name_list[i].title()) 18 i += 1 19 20 print('\n'.join(t))
运行测试截图
实验任务4
task4
实验源码
1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] 2 name_list.sort() 3 i = 0 4 for n in name_list: 5 name_list[i] = n.title() 6 i = i + 1 7 for index, name in enumerate(name_list): 8 m = index + 1 9 print(m, '.', name)
运行测试截图
实验任务5
task5
实验源码
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 line = 0 23 word = 0 24 string = 0 25 space = 0 26 line = len(text.splitlines()) 27 word = len(text.split()) 28 string = len(text) 29 for i in text: 30 if i ==' ': 31 space+=1 32 print('行数:{}'.format(line)) 33 print('单词数:{}'.format(word)) 34 print('字符数:{}'.format(string)) 35 print('空格数:{}'.format(space))
运行测试截图
实验任务6
task6
实验源码
1 book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'], 2 ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'], 3 ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'], 4 ['来自民间的叛逆', '袁越', '','新星出版社'], 5 ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], 6 ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'], 7 ['小行星掉在下午','沈大成', '', '广西师范大学出版社']] 8 x='图书信息' 9 print(x.center(40,'*')) 10 for i in range(1,8): 11 print(f'{i}.《{book_list[i-1][0]}》|{book_list[i-1][1]}|{book_list[i-1][3]}')
运行测试截图
实验任务7
task7
实验源码
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 num = 0 10 n = 0 11 for i in data: 12 k = i.split() 13 for m in k: 14 n += int(m) 15 num += 1 16 a = n / num 17 print('{:.2f}'.format(a))
运行测试截图
实验任务8
task8
实验源码
1 words_sensitive_list = ['张三', 'V字仇杀队', '杀'] 2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。', 3 '电影<V字仇杀队>从豆瓣下架了', 4 '娱乐至死'] 5 n = 0 6 for i in comments_list: 7 for n in words_sensitive_list: 8 i = i.replace(n,"*"*len(n)) 9 print(i)
运行测试截图
实验任务9
task9-1
实验源码
1 """ 2 家用电器销售系统 3 v1.1 4 """ 5 6 #欢迎信息 7 print('欢迎使用家用电器销售系统') 8 9 #产品信息列表 10 print('产品和价格信息如下:') 11 print('**********************************************************') 12 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量') 13 print('----------------------------------------------') 14 print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%10.2f'%5999.00,'%10d'%20) 15 print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%10.2f'%6998.00,'%10d'%15) 16 print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%10.2f'%1999.00,'%10d'%10) 17 print('%-10s'%'0004','%-10s'%'空调','%-10s'%'格力','%10.2f'%3900.00,'%10d'%0) 18 print('%-10s'%'0005','%-10s'%'热水器','%-10s'%'美的','%10.2f'%688.00,'%10d'%30) 19 print('%-10s'%'0006','%-10s'%'笔记本','%-10s'%'联想','%10.2f'%5699.00,'%10d'%10) 20 print('%-10s'%'0007','%-10s'%'微波炉','%-10s'%'苏泊尔','%10.2f'%480.50,'%10d'%33) 21 print('%-10s'%'0008','%-10s'%'投影仪','%-10s'%'松下','%10.2f'%1250.00,'%10d'%12) 22 print('%-10s'%'0009','%-10s'%'吸尘器','%-10s'%'飞利浦','%10.2f'%999.00,'%10d'%9) 23 print('----------------------------------------------') 24 25 #商品数据 26 products=[ 27 ['0001','电视机','海尔',5999.00,20], 28 ['0002','冰箱','西门子',6998.00,15], 29 ['0003''洗衣机','小天鹅',1999.00,10], 30 ['0004''空调','格力',3900.00,0], 31 ['0005''热水器','美的',688.00,30], 32 ['0006''笔记本','联想',5699.00,10], 33 ['0007','微波炉','苏泊尔',1250.00,12], 34 ['0008','投影仪','松下',1250.00,12], 35 ['0009','吸尘器','飞利浦',999.00,9], 36 ] 37 38 #用户输入信息 39 product_id=input('请输入您要购买的产品编号:') 40 count=int(input('请输入您要购买的产品数量:')) 41 42 #获取编号对应商品的信息 43 product_index=int(product_id)-1 44 product=products[product_index] 45 46 #计算金额 47 print('购买成功,您需要支付',product[3]*count,'元') 48 49 #退出系统 50 print('谢谢您的光临,下次再见')
运行测试截图
task9-2
实验源码
1 """家用电器销售系统 2 3 v1.1 4 """ 5 6 #欢迎信息 7 print('欢迎使用家用电器销售系统') 8 9 #产品信息列表 10 print('产品和价格信息如下:') 11 print('**********************************************************') 12 print(f'{"编号":<10}{"名称":<10}{"品牌":<10}{"价格":<10}{"库存数量":<10}') 13 print('----------------------------------------------') 14 print(f'{"0001":<10}{"电视机":<10}{"海尔":<10}{5999.00:>10.2f}{20:>10d}') 15 print(f'{"0002":<10}{"冰箱":<10}{"西门子":<10}{6998.00:>10.2f}{15:>10d}') 16 print(f'{"0003":<10}{"洗衣机":<10}{"小天鹅":<10}{1999.00:>10.2f}{10:>10d}') 17 print(f'{"0004":<10}{"空调":<10}{"格力":<10}{3900.00:>10.2f}{0:>10d}') 18 print(f'{"0005":<10}{"热水器":<10}{"美的":<10}{688.00:>10.2f}{30:>10d}') 19 print(f'{"0006":<10}{"笔记本":<10}{"联想":<10}{5699.00:>10.2f}{10:>10d}') 20 print(f'{"0007":<10}{"微波炉":<10}{"苏泊尔":<10}{480.50:>10.2f}{33:>10d}') 21 print(f'{"0008":<10}{"投影仪":<10}{"松下":<10}{1250.00:>10.2f}{12:>10d}') 22 print(f'{"0009":<10}{"吸尘器":<10}{"飞利浦":<10}{999.00:>10.2f}{9:>10d}') 23 print('----------------------------------------------') 24 25 #商品数据 26 products=[ 27 ['0001','电视机','海尔',5999.00,20], 28 ['0002','冰箱','西门子',6998.00,15], 29 ['0003''洗衣机','小天鹅',1999.00,10], 30 ['0004''空调','格力',3900.00,0], 31 ['0005''热水器','美的',688.00,30], 32 ['0006''笔记本','联想',5699.00,10], 33 ['0007','微波炉','苏泊尔',1250.00,12], 34 ['0008','投影仪','松下',1250.00,12], 35 ['0009','吸尘器','飞利浦',999.00,9], 36 ] 37 38 39 #用户输入信息 40 product_id=input('请输入您要购买的产品编号:') 41 count=int(input('请输入您要购买的产品数量:')) 42 43 #获取编号对应商品的信息 44 product_index=int(product_id)-1 45 product=products[product_index] 46 47 #计算金额 48 print('购买成功,您需要支付',product[3]*count,'元') 49 50 #退出系统 51 print('谢谢您的光临,下次再见')
运行测试截图
标签:%-,10,list,10d,列表,print,实验,字符串,10s From: https://www.cnblogs.com/siyi5981/p/17242964.html