实验2 字符串与列表

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

实验任务1

task1

运行源代码

 1 #task1.py
 2 print('task1.py')
 3 
 4 # 字符串的基础操作
 5 # 课堂上没有演示的一些方法
 6 
 7 x = 'nba FIFA'
 8 print(x.upper()) # 字符串转大写
 9 print(x.lower()) # 字符串转小写
10 print(x.swapcase()) # 字符串大小写翻转
11 print()
12 
13 x = 'abc'
14 print(x.center(10, '*')) # 字符串居中,宽度10列,不足左右补*号
15 print(x.ljust(10, '*')) # 字符串居左,宽度10列,不足右边补*号
16 print(x.rjust(10, '*')) # 字符串居右,宽度10列,不足左边补*号
17 print()
18 
19 x = '123'
20 print(x.zfill(10)) # 字符串宽度10列,不足左边用0填充
21 x = 123
22 print(str(x).zfill(10)) # 把int类型转换成字符串类型后,对字符串对象使用str.zfill()方法
23 print()
24 
25 x = 'phone_number'
26 print(x.isidentifier()) # 判断字符串是否是python合法标识符
27 x = '222test'
28 print(x.isidentifier())
29 print()
30 
31 x = ' '
32 print(x.isspace()) # 判断字符串是否是空白符(包括空格、回车、Tab键)
33 x = '\n'
34 print(x.isspace())
35 print()
36 
37 x = 'python is fun'
38 table = x.maketrans('thon', '1234') # 为字符串对象x创建一个字符映射表, 字符thon分别映射到字符1234
39 print(x.translate(table)) # 根据字符映射表table对字符串对象x中的字符进行转换

 

运行截图

 

 

实验任务2

task2

程序源代码

 1 #task2.py
 2 
 3 print('task2.py')
 4 
 5 #基础练习:列表、格式化、类型转换
 6 
 7 x = [5,11,9,7,42]
 8 
 9 print('整数输出1: ', end ='')
10 i = 0
11 while i < len(x):
12     print(x[i], end ='')
13     i += 1
14 
15 
16 print('\n整数输出2: ', end = '')
17 i = 0
18 while i< len(x):
19     print(f'{x[i]:02d}', end = '')#指定每个整数宽度占2列:不足2列,左边补0
20     i += 1
21 
22 
23 print('\n整数输出3: ', end = '')
24 i = 0
25 while i < len(x)-1:
26     print(f'{x[i]:02d}', end = '-')
27     i += 1
28 print(f'{x[-1]:02d}')
29 
30 
31 print('\n字符输出1: ', end = '')
32 y1 = []
33 i = 0
34 while i < len(x):
35     y1.append(str(x[i]))  #函数str()用于把其他类型对象转换成字符串对象
36     i += 1
37 print('-'.join(y1))
38 
39 
40 print('字符输出2: ', end = '')
41 y2 = []
42 i = 0
43 while i < len(x):
44     y2.append( str(x[i]).zfill(2) )#x[i]是int类型对象,使用str()转换成字符串对象后,处理,然后加入到列表y中
45     i += 1
46 print('-'.join(y2))

 

运行截图

 

 

实验任务3

task3

程序源代码

 1 #task3.py
 2 print('task3.py')
 3 
 4 #把姓名转换成大写,遍历分行输出
 5 
 6 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteau twins']
 7 
 8 # 方法1
 9 i = 0
10 while i < len(name_list):
11     print(name_list[i].title())
12     i += 1
13 print()
14 
15 # 方法2
16 t = []
17 i = 0
18 while i < len(name_list):
19     t.append(name_list[i].title())
20     i += 1
21 
22 print('\n'.join(t))

 

运行截图

 

 

实验任务4

task4

运行源代码

 1 #task4.py
 2 print('task4.py')
 3 
 4 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins']
 5 i = 1
 6 while i <= len(name_list):
 7     name_list.sort()
 8     print(f'{i}.' , end = '')
 9     print(name_list[i-1].title())
10     i += 1

 

 

 

 

实验任务5

task5

程序源代码

 1 #task5
 2 print('task5')
 3 file = '''
 4 The Zen of Python, by Tim Peters
 5 
 6 Beautiful is better than ugly.
 7 Explicit is better than implicit.
 8 Simple is better than complex.
 9 Complex is better than complicated.
10 Flat is better than nested.
11 Sparse is better than dense.
12 Readability counts.
13 Special cases aren't special enough to break the rules.
14 Although practicality beats purity.
15 Errors should never pass silently.
16 Unless explicitly silenced.
17 In the face of ambiguity, refuse the temptation to guess.
18 There should be one-- and preferably only one --obvious way to do it.
19 Although that way may not be obvious at first unless you're Dutch.
20 Now is better than never.
21 Although never is often better than *right* now.
22 If the implementation is hard to explain, it's a bad idea.
23 If the implementation is easy to explain, it may be a good idea.
24 Namespaces are one honking great idea -- let's do more of those!'''
25 line = file.count('\n')
26 character = len(str.strip(file))
27 word = len(file.split())
28 space = file.count(' ')
29 print(f'行数:{line}')
30 print(f'单词数:{word}')
31 print(f'字符数:{character}')
32 print(f'空格数:{space}')

 

运行截图

 

 

 

实验任务6

task6

程序源代码

 1 #task6
 2 print('task6')
 3 
 4 book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'],
 5              ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'],
 6              ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'],
 7              ['来自民间的叛逆', '袁越', '','新星出版社'],
 8              ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'],
 9              ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'],
10              ['小行星掉在下午','沈大成', '', '广西师范大学出版社']]
11 print('{:*^36}'.format('图书信息'))
12 i = 0
13 while i < len(book_list):
14     x = book_list[i]
15     print(f'{i+1},', '|'.join(x))
16     i += 1

 

运行截图

 

 

 

实验任务7

task7

程序源代码

 1 #tak7
 2 print('task7')
 3 
 4 x = '''
 5 99 81 75
 6 30 42 90 87
 7 69 50 96 77 89, 93
 8 82, 99, 78, 100
 9 '''
10 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
11 sum = 0
12 count = 0
13 for i in data:
14     group = i.split()
15     for num in group:
16         count +=1
17         sum +=int(num)
18 print(f'{(sum/count):.2f}')

 

运行截图

 

 

 

 

实验任务8

task8

程序源代码

 1 #task8
 2 print('task8')
 3 
 4 words_sensitive_list = ['张三', 'V字仇杀队', '']
 5 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。','电影<V字仇杀队>从豆瓣下架了','娱乐至死']
 6 for i in words_sensitive_list:
 7     for j in range(0,len(comments_list)):
 8         if i in comments_list[j]:
 9             comments_list[j] = comments_list[j].replace(i,'*'*len(i))
10 for line in comments_list:
11     print(line)

 

运行截图

 

 

 

 

实验任务9

task9_1

程序源代码

 1 #task9_1.py
 2 print('task9_1.py')
 3 
 4 """
 5 家用电器销售系统
 6 v1.1
 7 """
 8 #欢迎信息
 9 print('欢迎使用家用电器销售系统!')
10 
11 #产品信息列表
12 print('产品和价格信息如下:')
13 print('***********************************************************')
14 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
15 print('-----------------------------------------------------------')
16 print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%10.2f'%5999.00,'%10d'%20)
17 print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%10.2f'%6998.00,'%10d'%15)
18 print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%10.2f'%1999.00,'%10d'%10)
19 print('%-10s'%'0004','%-10s'%'空调','%-10s'%'格力','%10.2f'%3900.00,'%10d'%0)
20 print('%-10s'%'0005','%-10s'%'热水器','%-10s'%'美的','%10.2f'%688.00,'%10d'%30)
21 print('%-10s'%'0006','%-10s'%'笔记本','%-10s'%'联想','%10.2f'%5699.00,'%10d'%10)
22 print('%-10s'%'0007','%-10s'%'微波炉','%-10s'%'苏泊尔','%10.2f'%480.50,'%10d'%33)
23 print('%-10s'%'0008','%-10s'%'投影仪','%-10s'%'松下','%10.2f'%1250.00,'%10d'%12)
24 print('%-10s'%'0009','%-10s'%'吸尘器','%-10s'%'飞利浦','%10.2f'%999.00,'%10d'%9)
25 print('-----------------------------------------------------------')
26 
27 #商品数据
28 products = [
29 ['0001','电视机','海尔',5999.00,20],
30 ['0002','冰箱','西门子',6998.00,15],
31 ['0003','洗衣机','小天鹅',1999.00,10],
32 ['0004','空调','格力',3900.00,0],
33 ['0005','热水器','美的',688.00,30],
34 ['0006','笔记本','联想',5699.00,10],
35 ['0007','微波炉','苏泊尔',480.50,33],
36 ['0008','投影仪','松下',1250.00,12],
37 ['0009','吸尘器','飞利浦',999.00,9]
38 ]
39 
40 #用户输入信息
41 product_id = input('请输入您要购买的产品编号:')
42 count = int(input('请输入您要购买的产品数量:'))
43 
44 #获取编号对应商品的信息
45 product_index = int(product_id) - 1
46 product = products[product_index]
47 
48 #计算金额
49 print('购买成功,您需要支付',product[3]*count,'')
50 
51 #退出系统
52 print('谢谢您的光临,下次再见!')

 

运行截图

 

 

 

task9_2

程序源代码

 1 #task9_2.py
 2 print('task9_2.py')
 3 
 4 """
 5 家用电器销售系统
 6 v1.1
 7 """
 8 #欢迎信息
 9 print('欢迎使用家用电器销售系统!')
10 
11 #商品数据
12 products = [
13 ['0001','电视机','海尔',5999.00,20],
14 ['0002','冰箱','西门子',6998.00,15],
15 ['0003','洗衣机','小天鹅',1999.00,10],
16 ['0004','空调','格力',3900.00,0],
17 ['0005','热水器','美的',688.00,30],
18 ['0006','笔记本','联想',5699.00,10],
19 ['0007','微波炉','苏泊尔',480.50,33],
20 ['0008','投影仪','松下',1250.00,12],
21 ['0009','吸尘器','飞利浦',999.00,9]
22 ]
23 
24 #产品信息列表
25 print('产品和价格信息如下:')
26 print('{:*^55}'.format('*'))
27 print('{:10}'.format('编号'),'{:10}'.format('名称'),'{:10}'.format('品牌'),'{:10}'.format('价格'),'{:10}'.format('库存数量'))
28 print('{:-^55}'.format('-'))
29 print('{:10}'.format(products[0][0]),'{:10}'.format(products[0][1]),'{:10}'.format(products[0][2]),'{:10}'.format(products[0][3]),'{:>10}'.format(products[0][4]))
30 print('{:10}'.format(products[1][0]),'{:10}'.format(products[1][1]),'{:10}'.format(products[1][2]),'{:10}'.format(products[1][3]),'{:>10}'.format(products[1][4]))
31 print('{:10}'.format(products[2][0]),'{:10}'.format(products[2][1]),'{:10}'.format(products[2][2]),'{:10}'.format(products[2][3]),'{:>10}'.format(products[2][4]))
32 print('{:10}'.format(products[3][0]),'{:10}'.format(products[3][1]),'{:10}'.format(products[3][2]),'{:10}'.format(products[3][3]),'{:>10}'.format(products[3][4]))
33 print('{:10}'.format(products[4][0]),'{:10}'.format(products[4][1]),'{:10}'.format(products[4][2]),'{:10}'.format(products[4][3]),'{:>10}'.format(products[4][4]))
34 print('{:10}'.format(products[5][0]),'{:10}'.format(products[5][1]),'{:10}'.format(products[5][2]),'{:10}'.format(products[5][3]),'{:>10}'.format(products[5][4]))
35 print('{:10}'.format(products[6][0]),'{:10}'.format(products[6][1]),'{:10}'.format(products[6][2]),'{:10}'.format(products[6][3]),'{:>10}'.format(products[6][4]))
36 print('{:10}'.format(products[7][0]),'{:10}'.format(products[7][1]),'{:10}'.format(products[7][2]),'{:10}'.format(products[7][3]),'{:>10}'.format(products[7][4]))
37 print('{:10}'.format(products[8][0]),'{:10}'.format(products[8][1]),'{:10}'.format(products[8][2]),'{:10}'.format(products[8][3]),'{:>10}'.format(products[8][4]))
38 print('{:-^55}'.format('-'))
39 
40 #用户输入信息
41 product_id = input('请输入您要购买的产品编号:')
42 count = int(input('请输入您要购买的产品数量:'))
43 
44 #获取编号对应商品的信息
45 product_index = int(product_id) - 1
46 product = products[product_index]
47 
48 #计算金额
49 print('购买成功,您需要支付',product[3]*count,'')
50 
51 #退出系统
52 print('谢谢您的光临,下次再见!')

 

运行截图

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6