首页 > 其他分享 >实验2

实验2

时间:2023-03-27 23:13:07浏览次数:29  
标签:%- 10 name list 实验 print 10s

1. 实验任务1
task1.py

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类型转换成字符串类型后,对字符串对象使用str.zfill()
方法
print()

实验截图:

 

 

 

x = 'phone_number'
print(x.isidentifier()) # 判断字符串是否是python合法标识符
x = '222test'
print(x.isidentifier())
print()
x = ' '
print(x.isspace()) # 判断字符串是否是空白符(包括空格、回车、Tab键)
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

 

 

 1 x = [5,11,9,7,42]
 2 
 3 print('整数输出1: ', end = '')
 4 i = 0
 5 while  i < len(x):
 6     print(x[i],end = ' ')
 7     i += 1
 8 
 9 
10 print('\n整数输出2: ', end = '')
11 i = 0
12 while  i < len(x):
13     print(f'{x[i]:02d}', end = '-')
14     i += 1
15 
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[-1]:02d}')
23 
24 
25 print('\n字符输出1: ',end = '')
26 y1 = []
27 i = 0
28 while  i < len(x):
29     y1.append(str(x[i]))
30     i += 1
31 print('-'.join(y1))
32 
33 
34 print('字符输出2: ',end = '')
35 y2 = []
36 i = 0
37 while  i < len(x):
38     y2.append(str(x[i]).zfill(2))
39     i += 1
40 print('-'.join(y2))

 

 实验截图:

 

 

 

 

 

 3. 实验任务3
字符串方法 .title() 和 .join() 用法
task3.py

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

 

 1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan',
 2 'cocteau twins']
 3 
 4 t = []
 5 i = 0
 6 while i < len(name_list):
 7     t.append(name_list[i].title())
 8     i += 1
 9 
10 print('\n'.join(t))

 

实验截图:

 

4. 实验任务4
task4.py

 

1 name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
2 name_list = sorted(name_list)
3 i = 1
4 for name in name_list:
5      print("%d."%(i),end = " ")
6      print(name.title())
7      i+=1

实验截图:

 

 5. 实验任务5

task5.py

 1 import this
 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 line = 0
24 word = 0
25 string = 0
26 space = 0
27 line = len(text.splitlines())
28 word = len(text.split())
29 string = len(text)
30 for i in text:
31      if i ==' ':
32          space+=1
33 print('行数:{}'.format(line))
34 print('单词数:{}'.format(word))
35 print('字符数:{}'.format(string))
36 print('空格数:{}'.format(space))

实验截图:

 

 6. 实验任务6

task6.py

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

实验截图:

 

 7. 实验任务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 num = 0
10 n = 0
11 for i in data:
12     k = i.split()
13     for m in k:
14         n = n + int(m)
15         num = num+1
16 a = n / num
17 print('{:.2f}'.format(a))

实验截图:

 

 8. 实验任务8

task8.py

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

实验截图:

 

 9. 实验任务9

task9.py

 1 """
 2 家用电器销售系统
 3 v1.1
 4 """
 5 
 6 
 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','微波炉','苏泊尔',1250.00,12],
36     ['0008','投影仪','松下',1250.00,12],
37     ['0009','吸尘器','飞利浦',999.00,9],
38 ]
39 
40 
41 #用户输入信息
42 product_id=input('请输入您要购买的产品编号:')
43 count=int(input('请输入您要购买的产品数量:'))
44 
45 
46 #获取编号对应商品的信息
47 product_index=int(product_id)-1
48 product=products[product_index]
49 
50 
51 
52 #计算金额
53 print('购买成功,您需要支付',product[3]*count,'元')
54 
55 
56 #退出系统
57 print('谢谢您的光临,下次再见')

实验截图:

 

标签:%-,10,name,list,实验,print,10s
From: https://www.cnblogs.com/zyaiyj-www-2003/p/17242837.html

相关文章

  • 实验二——字符串,列表
    任务一程序:1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())#大小写翻转56x='abc'7print(x.center(10,'*'))8pri......
  • 实验一 密码引擎-2-电子钥匙功能测试
    目录1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图......
  • 实验一 密码引擎-2-电子钥匙功能测试
    在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图加分项:运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000......
  • 实验一 密码引擎-2-电子钥匙功能测试
    任务详情0参考附件中的视频1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_ma......
  • 202031604107-米乐文 实验一 软件工程准备—初识软件工程
    项目内容课程班级博客链接2020级卓越工程师班这个作业要求链接实验一——软件工程准备我的课程学习目标1.学会使用博客园进行学习2.了解Github工具的......
  • 实验二 字符串和列表
    task1源代码#字符串基本操作x='nbaFIFA'print(x.upper())#转大写print(x.lower())#转小写print(x.swapcase())#大小写翻转print()x='abc'print(......
  • 实验二
    1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())5print()67x='abc'8print(x.center(10,'*'))9print(x.ljust(10,'*......
  • 实验2
    task1.pyx='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(10,'*'))print(x.rjust(10,'......
  • 实验二
    task1源代码#字符串的基础操作x='nbaFIFA'print(x.upper())#字符串转大写print(x.lower())#字符串转小写print(x.swapcase())#字符串大小写翻转print()x......
  • 实验2 字符串和列表
    1.试验任务11x='nbaFIFA'2print(x.upper())#字符串转大写3print(x.lower())#字符串转小写4print(x.swapcase())#字符串大小写互换5print()6......