3-3 自己的列表:想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如"I would like to own a Honda motorcycle"。
vehicles = ['bicycle', 'subway']
message = "I prefer riding the ofo " + vehicles[0] + "."
print(message)
message = "I would like to take the " + vehicles[1] + " to go out."
print(message)
输出:
I prefer riding the ofo bicycle.
I would like to take the subway to go out.
3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
3-6 添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。
3-7 缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。
people = ['Ma Huateng', 'Lei Jun', 'Liu Qiangdong']
for person in people :
print(person + ", I'm glad to invite you to have a dinner with me tonight!")
print("\nOh, I just found a bigger table!")
people.insert(0, 'Mark Zuckerberg')
people.insert(2, 'Jeff Bezos')
people.append('Bill Gates')
for person in people :
print(person + ", I'm glad to invite you to have a dinner with me tonight!")
print("\nOh, I just knew that my new table could not be delivered in time!")
while len(people) > 2 :
print("I'm sorry I can't invite you to dinner, " + people.pop() + '.')
for person in people :
print(person + ", you are still in the list of invitees.")
del people[0]
del people[0]
print(people)
输出:
Ma Huateng, I'm glad to invite you to have a dinner with me tonight!
Lei Jun, I'm glad to invite you to have a dinner with me tonight!
Liu Qiangdong, I'm glad to invite you to have a dinner with me tonight!
Oh, I just found a bigger table!
Mark Zuckerberg, I'm glad to invite you to have a dinner with me tonight!
Ma Huateng, I'm glad to invite you to have a dinner with me tonight!
Jeff Bezos, I'm glad to invite you to have a dinner with me tonight!
Lei Jun, I'm glad to invite you to have a dinner with me tonight!
Liu Qiangdong, I'm glad to invite you to have a dinner with me tonight!
Bill Gates, I'm glad to invite you to have a dinner with me tonight!
Oh, I just knew that my new table could not be delivered in time!
I'm sorry I can't invite you to dinner, Bill Gates.
I'm sorry I can't invite you to dinner, Liu Qiangdong.
I'm sorry I can't invite you to dinner, Lei Jun.
I'm sorry I can't invite you to dinner, Jeff Bezos.
Mark Zuckerberg, you are still in the list of invitees.
Ma Huateng, you are still in the list of invitees.
[]
places = ['Sydney', 'Tokyo', 'New York', 'Silicon Valley', 'London']
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)
输出:
['Sydney', 'Tokyo', 'New York', 'Silicon Valley', 'London']
['London', 'New York', 'Silicon Valley', 'Sydney', 'Tokyo']
['Sydney', 'Tokyo', 'New York', 'Silicon Valley', 'London']
['Tokyo', 'Sydney', 'Silicon Valley', 'New York', 'London']
['Sydney', 'Tokyo', 'New York', 'Silicon Valley', 'London']
['London', 'Silicon Valley', 'New York', 'Tokyo', 'Sydney']
['Sydney', 'Tokyo', 'New York', 'Silicon Valley', 'London']
['London', 'New York', 'Silicon Valley', 'Sydney', 'Tokyo']
['Tokyo', 'Sydney', 'Silicon Valley', 'New York', 'London']