教材第三章练习题

前言

必须指出的是,由于在第二章内书中并未提及for循环的使用,于是我在此博客中提到的代码均未使用循环语句。但实际上,使用循环语句会使得代码更为简洁。例如3-4题中,用for循环版本的代码如下

names = ['Jobs','Liu Bei','Jin Hangyu']
for name in names:
	print('I would like to have a dinner with you, ' + name)

3-1 姓名

names = ['Jin','Hang','Yu']
print(names[0])
print(names[1])
print(names[2])

输出

Jin
Hang
Yu

3-2 问候语

names = ['Jin','Hang','Yu']
print(names[0] + ', nice to see you!')
print(names[1] + ', nice to see you!')
print(names[2] + ', nice to see you!')

输出

Jin, nice to see you!
Hang, nice to see you!
Yu, nice to see you!

3-3 自己的列表

ways = ['motorcycle','bicycle','car','bus']
print('I would like to own a '+ ways[0])
print('I would like to own a '+ ways[1])
print('I would like to own a '+ ways[2])
print('I would like to own a '+ ways[3])

输出

I would like to own a motorcycle
I would like to own a bicycle
I would like to own a car
I would like to own a bus

3-4 嘉宾名单

names = ['Jobs','Liu Bei','Jin Hangyu']
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])

输出

I would like to have a dinner with you, Jobs
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu

3-5 修改嘉宾名单

names = ['Jobs','Liu Bei','Jin Hangyu']
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])
print('Oh, we just knew that ' + names[0] + ' could not attend our dinner.')
names[0] = 'Ben Borad'
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])

输出

I would like to have a dinner with you, Jobs
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu
Oh, we just knew that Jobs could not attend our dinner.
I would like to have a dinner with you, Ben Borad
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu

3-6 添加嘉宾

names = ['Jobs','Liu Bei','Jin Hangyu']
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])
print('Oh, we just found a larger table, which can allow us to invite three more people.')
names.insert(0,'Ben Board')
names.insert(2,'Yan Hao')
names.append('Xu Xiaotian')
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])
print('I would like to have a dinner with you, '+ names[3])
print('I would like to have a dinner with you, '+ names[4])
print('I would like to have a dinner with you, '+ names[5])

输出

I would like to have a dinner with you, Jobs
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu
Oh, we just found a larger table, which can allow us to invite three more people.
I would like to have a dinner with you, Ben Board
I would like to have a dinner with you, Jobs
I would like to have a dinner with you, Yan Hao
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu
I would like to have a dinner with you, Xu Xiaotian

3-7 缩减名单

names = ['Jobs','Liu Bei','Jin Hangyu']
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])
print('Oh, we just found a larger table, which can allow us to invite three more people.')
names.insert(0,'Ben Board')
names.insert(2,'Yan Hao')
names.append('Xu Xiaotian')
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])
print('I would like to have a dinner with you, '+ names[3])
print('I would like to have a dinner with you, '+ names[4])
print('I would like to have a dinner with you, '+ names[5])
print('Oh, I\'m very sorry that the table can not arrive on time, so I can only invite two people.')
print(names.pop() + ', I am very sorry that you can\'t come to my dinner.')
print(names.pop() + ', I am very sorry that you can\'t come to my dinner.')
print(names.pop() + ', I am very sorry that you can\'t come to my dinner.')
print(names.pop() + ', I am very sorry that you can\'t come to my dinner.')
print(names[0] + ', I am very glad that you can come to my dinner.')
print(names[1] + ', I am very glad that you can come to my dinner.')
del names[1]
del names[0]
print(names)

输出

I would like to have a dinner with you, Jobs
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu
Oh, we just found a larger table, which can allow us to invite three more people.
I would like to have a dinner with you, Ben Board
I would like to have a dinner with you, Jobs
I would like to have a dinner with you, Yan Hao
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu
I would like to have a dinner with you, Xu Xiaotian
Oh, I'm very sorry that the table can not arrive on time, so I can only invite two people.
Xu Xiaotian, I am very sorry that you can't come to my dinner.
Jin Hangyu, I am very sorry that you can't come to my dinner.
Liu Bei, I am very sorry that you can't come to my dinner.
Yan Hao, I am very sorry that you can't come to my dinner.
Ben Board, I am very glad that you can come to my dinner.
Jobs, I am very glad that you can come to my dinner.
[]

3-8 放眼世界

places = ['Bei Jing','Shang Hai','Hang Zhou','Shen Zhen','Guang Zhou']
print(places)
print(sorted(places))
print(places)
print(sorted(places,reverse=True))
print(places)
places.reverse()
print(places)
places.reverse()
print(places)
places.sort()
print(places)
places.sort(reverse=True)
print(places)

输出

['Bei Jing', 'Shang Hai', 'Hang Zhou', 'Shen Zhen', 'Guang Zhou']
['Bei Jing', 'Guang Zhou', 'Hang Zhou', 'Shang Hai', 'Shen Zhen']
['Bei Jing', 'Shang Hai', 'Hang Zhou', 'Shen Zhen', 'Guang Zhou']
['Shen Zhen', 'Shang Hai', 'Hang Zhou', 'Guang Zhou', 'Bei Jing']
['Bei Jing', 'Shang Hai', 'Hang Zhou', 'Shen Zhen', 'Guang Zhou']
['Guang Zhou', 'Shen Zhen', 'Hang Zhou', 'Shang Hai', 'Bei Jing']
['Bei Jing', 'Shang Hai', 'Hang Zhou', 'Shen Zhen', 'Guang Zhou']
['Bei Jing', 'Guang Zhou', 'Hang Zhou', 'Shang Hai', 'Shen Zhen']
['Shen Zhen', 'Shang Hai', 'Hang Zhou', 'Guang Zhou', 'Bei Jing']

3-9 晚餐嘉宾

names = ['Jobs','Liu Bei','Jin Hangyu']
print('I would like to have a dinner with you, '+ names[0])
print('I would like to have a dinner with you, '+ names[1])
print('I would like to have a dinner with you, '+ names[2])
print('Totally, I have invited ' + str(len(names)) + ' people to come to my dinner.')

输出

I would like to have a dinner with you, Jobs
I would like to have a dinner with you, Liu Bei
I would like to have a dinner with you, Jin Hangyu
Totally, I have invited 3 people to come to my dinner.

附录:知识点

列表:
list =[‘ ‘,’ ‘,’ ‘]
list[0]访问,可直接修改
list.append()末尾添加
list.insert(0,’ ‘ )在0处插入
list.pop()删除末尾元素
list.del(0,’ ‘)删除第0个元素

list.sort()按字母顺序排列
list.sort(reverse=Ture)按倒序排列

sorted(list)临时排列,不改变原列表的顺序
sorted(list,reverse=Ture)倒序临时排列

list.reverse()反转列表顺序
len(list)列表长度 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值