第二周作业_Chapter 3 课后练习

3-2 问候语

代码:

names = ['Mary','Tony','Nate']
message = names[0] + ", nice to meet you."
print(message)
message = names[1] + ", nice to meet you."
print(message)
message = names[2] + ", nice to meet you."
print(message)

运行结果:



3-5 修改嘉宾名单

代码:

lists = ['Mary','Tony','Nate']
message = lists[0] + ", would you come to have dinner?"
print(message)
message = lists[1] + ", would you come to have dinner?"
print(message)
message = lists[2] + ", would you come to have dinner?"
print(message)
message = "What a pity! " + lists[0] + " can't come.I'll invite Lily instead."
print(message)
lists[0] = 'Lily'
message = lists[0] + ", would you come to have dinner?"
print(message)
message = lists[1] + ", would you come to have dinner?"
print(message)
message = lists[2] + ", would you come to have dinner?"
print(message)

运行结果:



3-6 添加嘉宾

代码:

lists = ['Mary','Tony','Nate']
message = lists[0] + ", would you come to have dinner?"
print(message)
message = lists[1] + ", would you come to have dinner?"
print(message)
message = lists[2] + ", would you come to have dinner?"
print(message)
print("I am going to invite more guests.")
lists.insert(0,'Lily')
lists.insert(2,'Tom')
lists.append('Adam')
for guest in lists:
   print(guest + ", would you come to have dinner?")

运行结果:


3-7 缩减名单

代码:

lists = ['Mary','Tony','Nate']
message = lists[0] + ", would you come to have dinner?"
print(message)
message = lists[1] + ", would you come to have dinner?"
print(message)
message = lists[2] + ", would you come to have dinner?"
print(message)
print("I am going to invite more guests.")
lists.insert(0,'Lily')
lists.insert(2,'Tom')
lists.append('Adam')
for guest in lists:
   print(guest + ", would you come to have dinner?")
print("For some reasons, I can only invite two guests now.")
print(lists[0] + ", I am sorry.")
lists.pop(0)
print(lists[0] + ", I am sorry.")
lists.pop(0)
print(lists[0] + ", I am sorry.")
lists.pop(0)
print(lists[0] + ", I am sorry.")
lists.pop(0)
print(lists[0] + " is still invited.")
print(lists[1] + " is still invited.")
del lists[0]
del lists[0]
print(lists)

运行结果:



3-8 放眼世界

代码:

places = ['Hangzhou','Tokyo','Paris','New York','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)

运行结果:



你可能感兴趣的:(第二周作业_Chapter 3 课后练习)