3-4 嘉宾名单
guests = ["Mike", "John", "Joe", "Amy", "Sarah"]
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
输出:
Dear Mike, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
3-5 修改嘉宾名单
guests = ["Mike", "John", "Joe", "Amy", "Sarah"]
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
print(
"Mike was unable to come to the appointment because of something sudden.")
guests[0] = "Hawking"
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
输出:
Dear Mike, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
Mike was unable to come to the appointment because of something sudden.
Dear Hawking, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
3-6 添加嘉宾
guests = ["Mike", "John", "Joe", "Amy", "Sarah"]
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
print(
"Mike was unable to come to the appointment because of something sudden.")
guests[0] = "Hawking"
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
guests.insert(0, "Newton")
guests.insert(3, "Einstein")
guests.append("Tesla")
print("\n")
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
输出:
Dear Mike, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
Mike was unable to come to the appointment because of something sudden.
Dear Hawking, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
Dear Newton, can I have dinner with you?
Dear Hawking, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Einstein, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
Dear Tesla, can I have dinner with you?
3-7 缩减名单
guests = ["Mike", "John", "Joe", "Amy", "Sarah"]
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
print(
"Mike was unable to come to the appointment because of something sudden.")
guests[0] = "Hawking"
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
guests.insert(0, "Newton")
guests.insert(3, "Einstein")
guests.append("Tesla")
print("\n")
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
print(
"I can only invite two guests because the new dining table is not delivered at a time."
)
while len(guests) != 2:
name = guests.pop()
print("Dear " + name + ", I'm sorry I can't invite you to dinner.")
print()
for guest in guests:
print("Dear " + guest + ", can I have dinner with you?")
while guests:
del guests[0]
if guests:
print("The list is not empty.")
else:
print("The list is empty.")
输出:
Dear John, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
Mike was unable to come to the appointment because of something sudden.
Dear Hawking, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Sarah, can I have dinner with you?
Dear Newton, can I have dinner with you?
Dear Hawking, can I have dinner with you?
Dear John, can I have dinner with you?
Dear Einstein, can I have dinner with you?
Dear Joe, can I have dinner with you?
Dear Amy, can I have dinner with you?
Dear Sarah, can I have dinner with you?
Dear Tesla, can I have dinner with you?
I can only invite two guests because the new dining table is not delivered at a time.
Dear Tesla, I'm sorry I can't invite you to dinner.
Dear Sarah, I'm sorry I can't invite you to dinner.
Dear Amy, I'm sorry I can't invite you to dinner.
Dear Joe, I'm sorry I can't invite you to dinner.
Dear Einstein, I'm sorry I can't invite you to dinner.
Dear John, I'm sorry I can't invite you to dinner.
Dear Newton, can I have dinner with you?
Dear Hawking, can I have dinner with you?
The list is empty.
3-8 放眼世界
places = ['Paris', 'London', 'New York', 'Mount Tai', 'Chicago']
print("The first: ", places)
print("First sort: ", sorted(places))
print(places)
print("reversal sort: ", sorted(places, reverse=True))
print(places)
places.reverse()
print("First reverse: ", places)
places.reverse()
print("Second reverse", places)
places.sort()
print(places)
places.sort(reverse=True)
print(places)
输出:
The first: ['Paris', 'London', 'New York', 'Mount Tai', 'Chicago']
First sort: ['Chicago', 'London', 'Mount Tai', 'New York', 'Paris']
['Paris', 'London', 'New York', 'Mount Tai', 'Chicago']
reversal sort: ['Paris', 'New York', 'Mount Tai', 'London', 'Chicago']
['Paris', 'London', 'New York', 'Mount Tai', 'Chicago']
First reverse: ['Chicago', 'Mount Tai', 'New York', 'London', 'Paris']
Second reverse ['Paris', 'London', 'New York', 'Mount Tai', 'Chicago']
['Chicago', 'London', 'Mount Tai', 'New York', 'Paris']
['Paris', 'New York', 'Mount Tai', 'London', 'Chicago']