3-1 姓名: 将一些朋友的姓名存储在一个列表中,并将其命名为names 。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
# answer_3-1
name=['died duck','fan','little xin','xiang']
print(name[0])
print(name[1])
print(name[2])
print(name[-1])
3-2 问候语: 继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
# answer_3-2
message=name[0].title()+','+'nice to meet you!'
print(message)
message=name[1].title()+','+'nice to meet you!'
print(message)
message=name[2].title()+','+'nice to meet you!'
print(message)
message=name[-1].title()+','+'nice to meet you!'
print(message)
3-3 自己的列表: 想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。
# answer_3-3
transport=["car","bicycle","motorcycle","plane","ship"]
declaration="I would like to own a Ferrari "+transport[0]+'.'
print(declaration)
3-4 嘉宾名单 :如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。# answer_3-4 3-5
favorites=["ml","yx","yh"]
welcome="\nI'd like to welcome "+favorites[0]+","+favorites[1]+" and "+favorites[2]+" for a dinner."
print(welcome)
print("\n"+favorites.pop(1)+" is busy,so he can't participate in the dinner.")
favorites.insert(1,"cx")
welcome="I'd like to welcome "+favorites[0]+","+favorites[1]+" and "+favorites[2]+" for a dinner."
print(welcome)
3-6 添加嘉宾 :你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。# answer_3-6
print("\nI find a larger table")
favorites.insert(0,"gm")
favorites.insert(2,"bq")
favorites.append("yx")
for favorite in favorites:
welcome1="I'd like to welcome "+favorite+" for a dinner."
print(welcome1)
3-7 缩减名单 :你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。# answer_3-7
print("\nThe table can't arrive on time.Two people will attend this dinner.")
print("I'm sorry "+favorites.pop()+" ,you can't attend.")
print("I'm sorry "+favorites.pop()+" ,you can't attend.")
print("I'm sorry "+favorites.pop()+" ,you can't attend.")
print("I'm sorry "+favorites.pop(1)+" ,you can't attend.")
print(favorites[0]+",you are in the list.")
print(favorites[1]+",you are in the list.")
del favorites[0]
del favorites[1]
print(favorites[0])
3-8 放眼世界 :想出至少5个你渴望去旅游的地方。# answer_3-8
travel_list=['beijin','shanghai','guangzhou','shenzhen','nanjing']
print("the original list:")
print(travel_list)
print("the sorted list:")
print(sorted(travel_list))
print("the original list:")
print(travel_list)
print("the sorted list:")
print(sorted(travel_list,reverse=True))
print("the original list:")
print(travel_list)
print("the reverse list:")
travel_list.reverse()
print(travel_list)
print("the reverse list:")
travel_list.reverse()
print(travel_list)
print("the sort list:")
travel_list.sort()
print(travel_list)
print("the sort list:")
travel_list.sort(reverse=True)
print(travel_list)
#answer 3_9
favorites=["ml","yx","yh"]
welcome="\nI'd like to welcome "+favorites[0]+","+favorites[1]+" and "+favorites[2]+" for a dinner."
print(welcome)
print("\n"+favorites.pop(1)+" is busy,so he can't participate in the dinner.")
favorites.insert(1,"cx")
welcome="I'd like to welcome "+favorites[0]+","+favorites[1]+" and "+favorites[2]+" for a dinner."
print(welcome)
print("\nthe number of visitors is:")
print(len(favorites))
#answer 3_10
mountains=["tai","hua","song","heng"]
print(mountains[0])
print(mountains[0].title())
print(mountains[-1])
print(mountains)
mountains[0]='jiuhua'
print(mountains)
mountains.append('changbai')
print(mountains)
mountains.insert(1,'huang')
print(mountains)
del mountains[1]
print(mountains)
poped_mountain=mountains.pop(2)
print(mountains)
print(poped_mountain)
mountains.remove('hua')
print(mountains)
mountains=["tai","hua","song","heng"]
print(mountains)
print(sorted(mountains))
mountains.reverse()
print(mountains)
mountains.sort()
print(mountains)
print(len(mountains))
4-1 比萨 :想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来。
#answer 4_1
pizzas=['New York Style','Chicago Style','California Style']
for pizza in pizzas:
print(pizza)
print('I like pepperoni pizza\n')
print(pizzas)
print('I really love pizza!')
4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。
#answer 4_2
animals=['dog','cat','pig']
for animal in animals:
print(animal)
print('A dog would make a great pet\n')
print('Any of these animals would make a great pet!')
4-3 数到20 :使用一个for 循环打印数字1~20(含)。
#answer 4_3
for i in range(1,21):
print(i)
4-4 一百万 :创建一个列表,其中包含数字1~1 000 000,再使用一个for 循环将这些数字打印出来(如果输出的时间太长,按Ctrl + C停止输出,或关闭输出窗口)。
#answer 4_4
million=list(range(1,1000001))
print(million)
4-5 计算1~1 000 000的总和 :创建一个列表,其中包含数字1~1 000 000,再使用min() 和max() 核实该列表确实是从1开始,到1 000 000结束的。另外,对这个列表调用函数sum() ,看看Python将一百万个数字相加需要多长时间。
#answer 4_5
million=list(range(1,1000001))
print(min(million))
print(max(million))
print(sum(million))
4-6 奇数 :通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出来。
#answer 4_6
ji=list(range(1,21,2))
for i in ji:
print(i)
4-7 3的倍数 :创建一个列表,其中包含3~30内能被3整除的数字;再使用一个for 循环将这个列表中的数字都打印出来。
#answer 4_7
san=list(range(3,31,3))
for x in san:
print(x)
4-8 立方 :将同一个数字乘三次称为立方。例如,在Python中,2的立方用2**3 表示。请创建一个列表,其中包含前10个整数(即1~10)的立方,再使用一个for 循环将这些立方数都打印出来。
#answer 4_8
lis=[]
for num in range(1,11):
li=num**3
lis.append(li)
print(lis)
4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。
#answer 4_9
lis=[num**3 for num in range(1,11)]
print(lis)
4-10
切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。#answer 4_10
ji=list(range(1,21,2))
for i in ji:
print(i)
print('The first three items in the list are:')
print(ji[0:3])
print('Three items fromthe middle of the list are:')
print(ji[5:8])
print('The last three items in the list are:')
print(ji[-3:])
4-11 你的比萨和我的比萨 :在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas 中,再完成如下任务。
#answer 4_11
pizzas=['New York Style','Chicago Style','California Style']
friend_pizzas=pizzas[:]
pizzas.append('liulian')
friend_pizzas.append('tiepan')
print('My favorite pizzas are:')
for pizza in pizzas:
print(pizza)
print("My friend's favorite pizzas are:")
for friend_pizza in friend_pizzas:
print(friend_pizza)
4-12 使用多个循环 :在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for 循环来打印列表。请选择一个版本的foods.py,在其中编写两个for 循环,将各个食品列表都打印出来。
#answer 4_12
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
for my_food in my_foods:
print(my_food)
print("\nMy friend's favorite foods are:")
for friend_food in friend_foods:
print(friend_food)
4-13
自助餐 :有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。#answer 4_13
foods=('rice','noddle','dumpling','egg','meat')
for food in foods:
print(food)
foods=('rice','noddle','dumpling','pock','fish')
for food in foods:
print(food)