4-1 比萨:想出至少三种你喜欢的比萨,将其名称储存在一个列表中,在使用循环将每种比萨名称都打印出来。
修改这个for循环,使其打印包含比萨名称的句子,而不是仅仅是比萨的名称,对于每种比萨,都显示一行 “I like pepperoni pizza”
在程序的末尾添加一行代码,它不在for循环中,指出你有多喜欢比萨。输出应包含针对每个比萨的消息,还有一个总结性的句子,如 I really love pizza!
favorite_pizzas = ['pepperoni', 'hawaiian', 'veggie']#打印所有比萨饼的名称。for pizza in favorite_pizzas:print(pizza)print("\n")for pizza in favorite_pizzas:print("I really love " + pizza + " pizza!")print("\nI really love pizza!")
输出:
pepperoni
hawaiian
veggie
I really love pepperoni pizza!
I really love hawaiian pizza!
I really love veggie pizza!
I really love pizza!
4-2 动物:想出三种有共同特征的动物,将这些动物的名称储存在一个列表中,再使用for循环将每种动物的名称都打印出来。
修改这个程序,使其针对每种动物都打印一个句子,如 “A dog would make a great pet"
在程序的末尾添加一行代码,指出这些动物的共同之处,如打印 ”Any of these animals would make a great pet!"
animal = ['老虎','狮子','豹子']for animals in animal:print(animals)print("\n")#每个动物都打印for animal_two in animal:print("A "+ animal_two + " would make a great pet")print("\n")print("Any of these animals would make a great pet")
输出:
老虎
狮子
豹子
A 老虎 would make a great pet
A 狮子 would make a great pet
A 豹子 would make a great pet
Any of these animals would make a great pet
4-3 数到20:使用一个for循环打印数字1~20(包涵20)
numbers = list(range(1, 21))for number in numbers:print(number)
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4-4 一百万 :创建一个列表,其中包含数字1~1 000 000,再使用一个for 循环将这些数字打印出来(如果输出的时间太长,按Ctrl + C停止输出,或关闭输出窗口)。
million_nums = list(range(1,1000000))for million_num in million_nums:print(million_nums)
4-5 计算1~1 000 000的总和 :创建一个列表,其中包含数字1~1 000 000,再使用min() 和max() 核实该列表确实是从1开始,到1 000 000结束的。另外,对这个列表调用函数sum() ,看看Python将一百万个数字相加需要多长时间。
nums = list(range(1,1000001))print(min(nums))print(max(nums))print(sum(nums))
输出:
1
1000000
500000500000
4-6 奇数 :通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出来。
#函数range()从2开始,然后不断地加2,直到达到或者超过终值(21)oddnums = list(range(1,21,2))for oddnum in oddnums:print(oddnum)
输出:
1
3
5
7
9
11
13
15
17
19
4-7 3的倍数 :创建一个列表,其中包含3~30内能被3整除的数字;再使用一个for 循环将这个列表中的数字都打印出来。
#函数range()从3开始,然后不断地加3,直到达到或者超过终值(31)threes = list(range(3, 31, 3))for number in threes:print(number)
输出:
3
6
9
12
15
18
21
24
27
30
4-8 立方 :将同一个数字乘三次称为立方。例如,在Python中,2的立方用2**3 表示。请创建一个列表,其中包含前10个整数(即1~10)的立方,再使用一个for 循环将这些立方数都打印出来。
cubes = []for number in range(1, 11):cube = number**3cubes.append(cube)for cube in cubes:print(cube)
输出:
1
8
27
64
125
216
343
512
729
1000
4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方。
cubes = [number**3 for number in range(1,11)]for cube in cubes:print(cube)
输出:
1
8
27
64
125
216
343
512
729
1000
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:”,再使用切片来打印列表末尾的三个元素。
nums = list(range(1,11))print(nums)#列表中的前三项是:three_num = nums[0:3]print("The first three items in the list are:" + str(three_num))#列表中间的三个项目是:middle = len(nums) // 2middle_num = nums[middle -2:middle+1]print("Three items from the middle of the list are:" + str(middle_num))#列表中的最后三项是:last_num = nums[-3:]print("The last three items in the list are:" + str(last_num))
输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The first three items in the list are:[1, 2, 3]
Three items from the middle of the list are:[4, 5, 6]
The last three items in the list are:[8, 9, 10]
4-11 你的比萨和我的比萨 :在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas 中,再完成如下任务。
在原来的比萨列表中添加一种比萨。在列表friend_pizzas 中添加另一种比萨。核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使用一个for 循环来打印第一个列表;打印消息“My friend’s favorite pizzas are:”,再使用一
个for 循环来打印第二个列表。
核实新增的比萨被添加到了正确的列表中。mypizzas = [‘beefpizza’,‘hampizza’,‘saladpizza’]利用切片进行拷贝列表,不会改变原有列表元素
favorite_pizzas = ['pepperoni', 'hawaiian', 'veggie']#复制列表friend_pizzas = favorite_pizzas[:]#添加一个比萨favorite_pizzas.append("meat lover's")friend_pizzas.append('pesto')print("My favorite pizzas are:")for pizza in favorite_pizzas:print("- " + pizza)print("\nMy friend's favorite pizzas are:")for pizza in friend_pizzas:print("- " + pizza)
输出:
My favorite pizzas are:
pepperonihawaiianveggiemeat lover’s
My friend’s favorite pizzas are:
pepperonihawaiianveggiepesto
4-12 使用多个循环 :在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for 循环来打印列表。请选择一个版本的foods.py,在其中编写两个for 循环,将各个食品列表都打印出来。
my_foods = ['pizza','kfc','必胜客']friend_food = my_foods[:]print("My favorite foods are:")for food in my_foods:print(food)
输出:
My favorite foods are:
pizza
kfc
必胜客
4-13 自助餐 :有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
使用一个for 循环将该餐馆提供的五种食品都打印出来。
尝试修改其中的一个元素,核实Python确实会拒绝你这样做。
餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块:给元组变量赋值,并使用一个for 循环将新元组的每个元素都打印出来。
menu_items = ( 'rockfish sandwich', 'halibut nuggets', 'smoked salmon chowder','salmon burger', 'crab cakes',)print("You can choose from the following menu items:")for item in menu_items:print("- " + item)menu_items = ('rockfish sandwich', 'halibut nuggets', 'smoked salmon chowder','black cod tips', 'king crab legs',)print("\nOur menu has been updated.")print("You can now choose from the following items:")for item in menu_items:print("- " + item)
输出:
You can choose from the following menu items:
rockfish sandwichhalibut nuggetssmoked salmon chowdersalmon burgercrab cakes
Our menu has been updated.
You can now choose from the following items:
rockfish sandwichhalibut nuggetssmoked salmon chowderblack cod tipsking crab legs