第二次作业(3.12/3.14)

2018-03-15  本文已影响0人  Pessimist_34ad

第三章练习

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

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

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

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

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

# 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个你渴望去旅游的地方。

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 循环将每种动物的名称都打印出来。

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 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。

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
上一篇 下一篇

猜你喜欢

热点阅读