3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为 names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
names = ['xiaoming','xiaohong','xiaofang']
for name in names:
print(name)
运行结果:
xiaoming
xiaohong
xiaofang
3-2 问候语: 继续使用练习 3-1 中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names = ['xiaoming','xiaohong','xiaofang']
for name in names:
print(name + ", nice to meet you!")
运行结果:
xiaoming, nice to meet you!
xiaohong, nice to meet you!
xiaofang, nice to meet you!
3-3 自己的列表: 想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。
cars = ['BMW','Audi','Volkswagen']
for car in cars:
print("I would like to own a " + car + " car!")
运行结果:
I would like to own a BMW car!
I would like to own a Audi car!
I would like to own a Volkswagen car!
3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少 3 个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
persons = ['Mayun','Mahuateng','Leijun']
for person in persons:
print(person + ", I want to invite you to have dinner with me.")
运行结果:
Mayun, I want to invite you to have dinner with me.
Mahuateng, I want to invite you to have dinner with me.
Leijun, I want to invite you to have dinner with me.
3-5 修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。
#以完成练习 3-4 时编写的程序为基础,在程序末尾添加一条 print 语句,指出哪位嘉宾无法赴约。
print('Sorry, ' + persons[2] + " couldn't come.")
#修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
persons[2] = 'Liuqiangdong'
#再次打印一系列消息,向名单中的每位嘉宾发出邀请。
for person in persons:
print(person + ", I want to invite you to have dinner with me.")
运行结果:
Sorry, Leijun couldn't come.
Mayun, I want to invite you to have dinner with me.
Mahuateng, I want to invite you to have dinner with me.
Liuqiangdong, I want to invite you to have dinner with me.
3-6 添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
#以完成练习 3-4 或练习 3-5 时编写的程序为基础,在程序末尾添加一条 print 语句,指出你找到了一个更大的餐桌。
print('I find a bigger table !')
#使用 insert()将一位新嘉宾添加到名单开头。
persons.insert(0,'Zhoujielun')
#使用insert()将一位新嘉宾添加到名单中间。
persons.insert(2,'Chengyixun')
#使用insert()将一位新嘉宾添加到名单末尾。
persons.append('Dengziqi')
#打印一系列消息,向名单中的每位嘉宾发出邀请。
for person in persons:
print(person + ", I want to invite you to have dinner with me.")
运行结果:
I find a bigger table !
Zhoujielun, I want to invite you to have dinner with me.
Mayun, I want to invite you to have dinner with me.
Chengyixun, I want to invite you to have dinner with me.
Mahuateng, I want to invite you to have dinner with me.
Liuqiangdong, I want to invite you to have dinner with me.
Dengziqi, I want to invite you to have dinner with me.
3-7 缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
#以完成练习 3-6 时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
print('Only two guests can be invited to dinner.')
#使用 pop()不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
while len(persons)>2:
print(persons.pop() + ", I can't invite you to have dinner with me.")
#对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
for person in persons:
print(person + ', you are still in my list.')
#使用 del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
del persons[0]
del persons[0]
print(persons)
运行结果:
Only two guests can be invited to dinner.
Dengziqi, I can't invite you to have dinner with me.
Liuqiangdong, I can't invite you to have dinner with me.
Mahuateng, I can't invite you to have dinner with me.
Chengyixun, I can't invite you to have dinner with me.
Zhoujielun, you are still in my list.
Mayun, you are still in my list.
[]
3-8 放眼世界:想出至少 5 个你渴望去旅游的地方。
#将这些地方存储在一个列表中,并确保其中元素不是按字母顺序排序的
city = ['Beijing','Shanghai','Chengdu','Nanjing','Xianggang']
#按原始排列顺序打印该列表
print(city)
#使用sorted()按字母顺序打印这个列表,同时不要修改它
print(sorted(city))
#再次打印该列表,核实排列顺序未改变
print(city)
#使用sorted()按字母顺序相反的顺序打印这个列表,同时不要修改它
print(sorted(city,reverse = True))
#再次打印该列表,核实排列顺序未改变
print(city)
#使用reserve()修改列表元素的排列顺序。打印该列表
city.reverse()
print(city)
#使用reserve()再次修改列表元素的排列顺序。打印该列表
#核实已经恢复到原来的排列顺序
city.reverse()
print(city)
#使用sort()按字母顺序打印这个列表
city.sort()
print(city)
#使用sort()按字母顺序相反的顺序打印这个列表
city.sort(reverse = True)
print(city)
运行结果:
['Beijing', 'Shanghai', 'Chengdu', 'Nanjing', 'Xianggang']
['Beijing', 'Chengdu', 'Nanjing', 'Shanghai', 'Xianggang']
['Beijing', 'Shanghai', 'Chengdu', 'Nanjing', 'Xianggang']
['Xianggang', 'Shanghai', 'Nanjing', 'Chengdu', 'Beijing']
['Beijing', 'Shanghai', 'Chengdu', 'Nanjing', 'Xianggang']
['Xianggang', 'Nanjing', 'Chengdu', 'Shanghai', 'Beijing']
['Beijing', 'Shanghai', 'Chengdu', 'Nanjing', 'Xianggang']
['Beijing', 'Chengdu', 'Nanjing', 'Shanghai', 'Xianggang']
['Xianggang', 'Shanghai', 'Nanjing', 'Chengdu', 'Beijing']
3-9 晚餐嘉宾:在完成练习 3-4~练习 3-7 时编写的程序之一中,使用 len()打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。
persons = ['Mayun','Mahuateng','Leijun']
for person in persons:
print(person + ", I want to invite you to have dinner with me.")
print('I have invited '+str(len(persons))+' persons.')
运行结果:
Mayun, I want to invite you to have dinner with me.
Mahuateng, I want to invite you to have dinner with me.
Leijun, I want to invite you to have dinner with me.
I have invited 3 persons.