第三章答案
练习3-1:
names = ["alice", "bob", "carol", "david", "eve"]
print(names[0])
print(names[1])
print(names[2])
print(names[3])
print(names[4])
练习3-2:
names = ["alice", "bob", "carol", "david", "eve"]
print(f"{names[0].title()}, hello!")
print(f"{names[1].title()}, hello!")
print(f"{names[2].title()}, hello!")
print(f"{names[3].title()}, hello!")
print(f"{names[4].title()}, hello!")
练习3-3:
motorcycles = ["honda", "yamaha", "suzuki"]
print(f"I would like to own a {motorcycles[0]} motorcycle.")
print(f"I would like to own a {motorcycles[1]} motorcycle.")
print(f"I would like to own a {motorcycles[2]} motorcycle.")
练习3-4:
names = ["alice", "bob", "carol"]
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
练习3-5:
names = ["alice", "bob", "carol"]
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
print(f"{names[0].title()} is busy!")
names[0] = "jack"
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
练习3-6:
names = ["alice", "bob", "carol"]
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
print(f"{names[0].title()} is busy!")
names[0] = "jack"
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
print("I find a bigger table!")
names.insert(0, "jason")
names.insert(2, "david")
names.append("albert")
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
print(f"I will invite {names[3].title()}")
print(f"I will invite {names[4].title()}")
print(f"I will invite {names[5].title()}")
练习3-7:
names = ["alice", "bob", "carol"]
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
print(f"{names[0].title()} is busy!")
names[0] = "jack"
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
print("I find a bigger table!")
names.insert(0, "jason")
names.insert(2, "david")
names.append("albert")
print(f"I will invite {names[0].title()}")
print(f"I will invite {names[1].title()}")
print(f"I will invite {names[2].title()}")
print(f"I will invite {names[3].title()}")
print(f"I will invite {names[4].title()}")
print(f"I will invite {names[5].title()}")
print("I just can invite two persons!")
print(f"sorry,{names.pop()}")
print(f"sorry,{names.pop()}")
print(f"sorry,{names.pop()}")
print(f"sorry,{names.pop()}")
print(f"{names[0].title()}, you are invited!")
print(f"{names[1].title()}, you are invited!")
del names[0]
del names[0]
print(names)
练习3-8:
cities = ["chongqing", "beijing", "shanghai", "guangzhou", "dalian"]
print(cities)
print(sorted(cities))
print(cities)
print(sorted(cities, reverse=True))
print(cities)
cities.reverse()
print(cities)
cities.reverse()
print(cities)
cities.sort()
print(cities)
cities.sort(reverse=True)
print(cities)
练习3-9:
names = ["alice", "bob", "carol"]
print(f"I invite {len(names)} persons")
练习3-10:
略
练习3-11:
略