names = ['chen', 'x', 'l']
print(names[0])
print(names[1])
print(names[2])
names = ['chen', 'x', 'l']
msg = "Hello, " + names[0].title() + "!"
print(msg)
msg = "Hello, " + names[1].title() + "!"
print(msg)
msg = "Hello, " + names[2].title() + "!"
print(msg)
输出:
Hello, Chen!
Hello, X!
Hello, L!
types = ['步行','地铁','公交']
msg = "我喜欢"
for type in types:
print(msg + type + "。")
输出:
我喜欢步行。
我喜欢地铁。
我喜欢公交。
guests = ['chen', 'nico', 'xia']
name = guests[0].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
name = guests[2].title()
print(name + ", please come to dinner.")
输出:
Chen, please come to dinner.
Nico, please come to dinner.
X, please come to dinner.
guests = ['chen', 'nico', 'xia']
name = guests[0].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
name = guests[2].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print("\nSorry, " + name + " can't make it to dinner.")
#nico无法到来让我们邀请gary
del(guests[1])
guests.insert(1, 'gary ')
#再次邀请
name = guests[0].title()
print("\n" + name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
name = guests[2].title()
print(name + ", please come to dinner.")
输出:
Chen, please come to dinner.
Nico, please come to dinner.
Xia, please come to dinner.
Sorry, Nico can’t make it to dinner.
Chen, please come to dinner.
Gary , please come to dinner.
Xia, please come to dinner.
guests =['chen', 'nico‘, 'xia']
name = guests[0].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
name = guests[2].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print("\nSorry, " + name + " can't make it to dinner.")
del(guests[1])
guests.insert(1, 'gary')
#再次邀请
name = guests[0].title()
print("\n" + name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
name = guests[2].title()
print(name + ", please come to dinner.")
#我们有一个更大的桌子,所以让我们在列表中添加更多人。
print("\nWe got a bigger table!")
#指定位置添加
guests.insert(0, 'A')
guests.insert(2, 'B')
#末尾添加
guests.append('C')
name = guests[0].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
name = guests[2].title()
print(name + ", please come to dinner.")
name = guests[3].title()
print(name + ", please come to dinner.")
name = guests[4].title()
print(name + ", please come to dinner.")
name = guests[5].title()
print(name + ", please come to dinner.")
输出:
Chen, please come to dinner.
Nico, please come to dinner.
Xia, please come to dinner.
Sorry, Nico can’t make it to dinner.
Chen, please come to dinner.
Gary, please come to dinner.
Xia, please come to dinner.
We got a bigger table!
A, please come to dinner.
Chen, please come to dinner.
B, please come to dinner.
Gary, please come to dinner.
Xia, please come to dinner.
C, please come to dinner.
接3-6
print("\nSorry, we can only invite two people to dinner.")
name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")
name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")
name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")
name = guests.pop()
print("Sorry, " + name.title() + " there's no room at the table.")
#应该还剩下两个人。 我们邀请他们。
name = guests[0].title()
print(name + ", please come to dinner.")
name = guests[1].title()
print(name + ", please come to dinner.")
#清空列表
del(guests[0])
del(guests[0])
print(guests)
输出:
Sorry, we can only invite two people to dinner.
Sorry, C there’s no room at the table.
Sorry, Xia there’s no room at the table.
Sorry, Gary there’s no room at the table.
Sorry, B there’s no room at the table.
A, please come to dinner.
Chen, please come to dinner.
[]
使用sorted() 按字母顺序打印这个列表,同时不要修改它。再次打印该列表,核实排列顺序未变。 使用sorted() 按与字母顺序相反的顺序打印这个列表,同时不要修改它。再次打印该列表,核实排列顺序未变。
使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。
使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。
locations = ['himalaya', 'andes', 'tierra del fuego', 'labrador', 'guam']
#原始列表打印
print("Original order:")
print(locations)
#使用sorted() 按字母顺序打印列表
print("\nAlphabetical:")
print(sorted(locations))
#再次打印该列表,核实排列顺序未变。
print("\nOriginal order:")
print(locations)
#使用sorted() 按与字母顺序相反的顺序打印这个列表,同时不要修改它。
print("\nReverse alphabetical:")
print(sorted(locations, reverse=True))
#再次打印该列表,核实排列顺序未变。
print("\nOriginal order:")
print(locations)
#使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
print("\nReversed:" )
locations.reverse()
print(locations)
#使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
print("\nOriginal order:" )
locations.reverse()
print(locations)
#使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。
print("\nAlphabetical")
locations.sort()
print(locations)
#使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。
print("\nReverse alphabetical" )
locations.sort(reverse=True)
print(locations)
输出:
[‘andes’, ‘guam’, ‘himalaya’, ‘labrador’, ‘tierra del fuego’]
Original order:
[‘himalaya’, ‘andes’, ‘tierra del fuego’, ‘labrador’, ‘guam’]
Reverse alphabetical:
[‘tierra del fuego’, ‘labrador’, ‘himalaya’, ‘guam’, ‘andes’]
Original order:
[‘himalaya’, ‘andes’, ‘tierra del fuego’, ‘labrador’, ‘guam’]
Reversed:
[‘guam’, ‘labrador’, ‘tierra del fuego’, ‘andes’, ‘himalaya’]
Original order:
[‘himalaya’, ‘andes’, ‘tierra del fuego’, ‘labrador’, ‘guam’]
Alphabetical
[‘andes’, ‘guam’, ‘himalaya’, ‘labrador’, ‘tierra del fuego’]
Reverse alphabetical
[‘tierra del fuego’, ‘labrador’, ‘himalaya’, ‘guam’, ‘andes’]