Python编程从入门到实践 -----第3章、列表简介(课后习题答案)

3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为names 。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。

 names = ['chen', 'x', 'l']
 print(names[0])
 print(names[1])
 print(names[2])

3-2 问候语: 继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。

 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!

3-3 自己的列表: 想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。

 types = ['步行','地铁','公交']
 msg = "我喜欢"
 for type in types:
          print(msg + type + "。")

输出:
我喜欢步行。
我喜欢地铁。
我喜欢公交。

3-4 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。

 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.

3-5 修改嘉宾名单 :你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。以完成练习3-4时编写的程序为基础,在程序末尾添加一条print 语句,指出哪位嘉宾无法赴约。修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。再次打印一系列消息,向名单中的每位嘉宾发出邀请。

 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.

3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print 语句,指出你找到了一个更大的餐桌。使用insert() 将一位新嘉宾添加到名单开头。使用insert() 将另一位新嘉宾添加到名单中间。使用append() 将最后一位新嘉宾添加到名单末尾。打印一系列消息,向名单中的每位嘉宾发出邀请。

 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-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。使用pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。使用del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。

 接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.
[]

3-8 放眼世界 :想出至少5个你渴望去旅游的地方。将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。 按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。

使用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’]

你可能感兴趣的:(Python编程从入门到实践 -----第3章、列表简介(课后习题答案))