# 基础操作练习: 列表及列表推导式、格式化、类型转换
x = list(range(10))
print('整数输出1: ', end='')
for i in x:
    print(i, end=' ')
print('\n整数输出2: ', end='')
for i in x:
    print(f'{i:02d}', end='-')    # 指定每个整数输出宽度占两列,不足两列,左边补0
print('\n整数输出3: ', end='')
for i in x[:-1]:
    print(f'{i:02d}', end='-')
print(f'{x[-1]:02d}')
print('\n字符输出1: ', end='')
y1 = [str(i)for i in range(10)]  # 函数str()用于把其它类型对象转换成字符串对象
print('-'.join(y1))
print('字符输出2: ', end='')
y2 = [str(i).zfill(2)for i in range(10)]  # 方法.zfill()用于对字符串进行格式化,指定宽度为2列,不足左边补0
print('-'.join(y2))

 

 实验任务2

# 把姓名转换成大写,遍历输出
name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteau twins']
# 实现方式1
for name in name_list:
    print(name.title())
print()
# 实现方式2
name_list_captilize = [name.title() for name in name_list]
print('\n'.join(name_list_captilize))

 

 实验任务3

# 将姓和名首字母转换成大写,将姓名按字典序编号输出。
name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins']
name_list2 = sorted(name_list)
n=1
for name in name_list2:
    print(f'{n}.{name.title()}')
    n+=1

 

 实验任务4

import this
text = '''The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!'''
print('行数:',len(text.splitlines()))
print('单词数:',len(text.split()))
print('字符数:',len(text))
print('空格数:',text.count(' '))

 实验任务5

# 图书列表中每行信息: 书名、作者、译者、出版社# 如果是国内作者,则译者字段为空串.对图书编号输出,输出信息和形式诸如编号. 《书名》|作者|出版社(忽略译者列)
book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'],
             ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'],
             ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'],
             ['来自民间的叛逆', '袁越', '','新星出版社'],
             ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'],
             ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'],
             ['小行星掉在下午','沈大成', '', '广西师范大学出版社']]
i = 0
for i in range(7):
    print(f'{i+1}. 《{book_list[i][0]}》|{book_list[i][1]}|{book_list[i][3]}')

实验任务6

# 计算得到这组数据的均值,并打印输出,要求结果显示两位小数。
# 某.csv格式数据文件内数据如下:
data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
data1 = []
for i in data:
    data1 += i.split()
print(data1)
data2 = list(map(int, data1))
print(data2)
a = sum(data2)
b = len(data2)
print(round(a/b, 2))

 

 实验任务7

words_sensitive_list = ['张三', 'V字仇杀队', '']
comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
                 '电影<V字仇杀队>从豆瓣下架了',
                 '娱乐至死']
comments_list = str(comments_list).replace('张三', '**')
comments_list = str(comments_list).replace('V字仇杀队', '*****')
comments_list = str(comments_list).replace('', '*')
print(comments_list)

 

 总结:理清思路,一步一步运用知识点构建代码。

 

 

posted on 2022-04-13 10:16  清橙往忆  阅读(9)  评论(3编辑  收藏  举报