实验2 字符串和列表

1.实验任务1

实验源码 task_1:

 

#字符串的基础操作


x = 'nba FIFA'
print(x.upper())  #字符串转大写
print(x.lower())  #字符串转小写
print(x.swapcase()) #字符串大小写翻转
print()


x = 'abc'
print(x.center(10,'*'))  #字符串居中,宽度10列,不足左右补*号
print(x.ljust(10,'*'))   #字符串居左,宽度10列,不足右边补*号
print(x.rjust(10,'*'))   #字符串居右,宽度10列,不足左边补*号
print()

x = '123'
print(x.zfill(10))  #字符串宽度10列,不足左边用0填充
x = 123
print(str(x).zfill(10)) #将int转为字符串类型
print()

x = ' '
print(x.isspace())  #判断字符串是否为空白符
x = '\n'
print(x.isspace())
print()

x = 'python is fun'
table = x.maketrans('thon','1234')#为字符串对象x创建一个字符映射表,字符thon分别映射到字符1234
print(x.translate(table)) #根据字符映射表table对字符串对象x中的字符进行转换

运行测试截图:

2.实验任务2

实验源码:task2.py

#列表、格式化、类型转换

x = [5,11,9,7,42]

print('整数输出1: ',end  = '')
i  = 0
while i < len(x):
    print(x[i],end = ' ')
    i += 1


print('\n整数输出2: ',end = '')
i = 0
while i < len(x):
    print(f'{x[i]:02d}',end = '-')
    i += 1


print('\n整数输出3:',end = '')
i = 0
while i < len(x) - 1:
    print(f'{x[i]:02d}',end = '-')
    i += 1
print(f'{x[-1]:02d}')


print('\n字符输出1:',end = '')
y1 = []
i = 0
while i < len(x):
    y1.append(str(x[i]))
    i += 1
print('-'.join(y1))


print('字符输出2: ', end = '')
y2 = []
i = 0
while i < len(x):
    y2.append(str(x[i]).zfill(2))
    i += 1
print('-'.join(y2))

运行测试截图:

令x = 【1,9,8,4,2,0,49】

运行结果如下:

 

3.实验任务3

实验源码:task3.py

#把姓名转换成大小写,遍历分行输出

name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']

#way 1
i = 0
while i < len(name_list):
    print(name_list[i].title())
    i += 1

print()


#way 2
t = []
i = 0
while i < len(name_list):
    t.append(name_list[i].title())
    i += 1

print('\n'.join(t))

运行测试截图:

 

4.实验任务4

实验源码 task4.py:将姓和名首字母转换成大写,将姓名按字典序编号输出。

1 name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
2 
3 i = 0
4 while i < len(name_list):
5     print(str(i+1) + '.' + name_list[i].title())
6     i += 1

运行测试截图:

 

5.实验任务5

实验源码task5.py 统计zen of python 的行数,单词数,字符数,空格数。

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

运行测试截图:

 

实验任务6:

实验源码:task6.py 处理图书信息

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

运行测试截图:

实验任务7:

实验源码:task7.py 求均值

 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 
10 sum = 0
11 count = 0
12 for i in data:
13     list = i.split()
14     for x in list:
15         sum += int(x)
16         count += 1
17 print('%.2f'%(sum/(count)))

运行测试截图:

 8.实验任务8:

实验源码:task8.py 对list中的对象进行替换

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

运行测试截图:

9.实验任务9 对实验1的家用电器销售系统的优化,增加了列表。

实验源码:task9_1:

 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_2:

实验源码:

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

运行测试截图:

 

posted @ 2023-03-22 20:51  desire666666  阅读(42)  评论(0编辑  收藏  举报