第二次作业(3.12/3.14)

第三章练习

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

>>> name = ['Ann', 'Bob', 'John', 'Africamonkey']
>>> for item in name:
...     print(item)
... 
Ann
Bob
John
Africamonkey

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

3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。

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

3-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。

  • 以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾共进晚餐的消息。
  • 使用pop() 不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
  • 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
  • 使用del 将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
# 3-4
invitation_list = ['A', 'B', 'C']
for item in invitation_list:
    print("Honorably invite you, Mr.%s" % item)
print()
# 3-6
invitation_list.insert(0, 'D')
invitation_list.insert(2, 'E')
invitation_list.append('F')
for item in invitation_list:
    print("Honorably invite you, Mr.%s" % item)
print()
# 3-7
while len(invitation_list) > 2:
    print("Dear %s, Sorry to inform you that we cannot have dinnder together." % invitation_list[-1])
    invitation_list.pop()
for item in invitation_list:
    print("Honorably invite you, Mr.%s" % item)
del(invitation_list[:])
print(invitation_list)

输出结果如下:

Honorably invite you, Mr.A
Honorably invite you, Mr.B
Honorably invite you, Mr.C

Honorably invite you, Mr.D
Honorably invite you, Mr.A
Honorably invite you, Mr.E
Honorably invite you, Mr.B
Honorably invite you, Mr.C
Honorably invite you, Mr.F

Dear F, Sorry to inform you that we cannot have dinnder together.
Dear C, Sorry to inform you that we cannot have dinnder together.
Dear B, Sorry to inform you that we cannot have dinnder together.
Dear E, Sorry to inform you that we cannot have dinnder together.
Honorably invite you, Mr.D
Honorably invite you, Mr.A
[]

3-8 放眼世界 :想出至少5个你渴望去旅游的地方。

  • 将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。
  • 按原始排列顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。
  • 使用sorted() 按字母顺序打印这个列表,同时不要修改它。
  • 再次打印该列表,核实排列顺序未变。
  • 使用sorted() 按与字母顺序相反的顺序打印这个列表,同时不要修改它。
  • 再次打印该列表,核实排列顺序未变。
  • 使用reverse() 修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
  • 使用reverse() 再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
  • 使用sort() 修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。
  • 使用sort() 修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。
my_list = ['America', 'United Kingdom', 'Japan', 'New Zealand', 'Switzerland']
print(my_list)
print(sorted(my_list))
print(sorted(my_list, reverse = True))
my_list.reverse()
print(my_list)
my_list.reverse()
print(my_list)
my_list.sort()
print(my_list)
my_list.sort(reverse=True)
print(my_list)

输出结果:

['America', 'United Kingdom', 'Japan', 'New Zealand', 'Switzerland']
['America', 'Japan', 'New Zealand', 'Switzerland', 'United Kingdom']
['United Kingdom', 'Switzerland', 'New Zealand', 'Japan', 'America']
['Switzerland', 'New Zealand', 'Japan', 'United Kingdom', 'America']
['America', 'United Kingdom', 'Japan', 'New Zealand', 'Switzerland']
['America', 'Japan', 'New Zealand', 'Switzerland', 'United Kingdom']
['United Kingdom', 'Switzerland', 'New Zealand', 'Japan', 'America']

第四章练习

4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。

  • 修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
  • 在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these animals would make a great pet!”这样的句子。
animal = ['pegion', 'hammingbird', 'goose']
for item in animal:
    print(item)
print("Any of these animals have wings.")
pegion
hammingbird
goose
Any of these animals have wings.

4-3 数到20 :使用一个for 循环打印数字1~20(含)。

for i in range(1, 21):
    print(i)

4-6 奇数 :通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出来。

list = [i for i in range(1, 21, 2)]
for item in list:
    print(item)

4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。

list = [i ** 3 for i in range(1, 10)]

4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。

  • 打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个元素。
  • 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素。
  • 打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素。
list = [i ** 3 for i in range(1, 10)]
print("The first three items in the list are:")
for i in list[:3]:
    print(i)
print("Three items from the middle of the list are:")
for i in list[2:5]:
    print(i)
print("The last three items in the list are:")
for i in list[-3:]:
    print(i)

输出结果

The first three items in the list are:
1
8
27
Three items from the middle of the list are:
27
64
125
The last three items in the list are:
343
512
729

你可能感兴趣的:(第二次作业(3.12/3.14))