4-1 比萨:想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for循环将每种比萨的名称都打印出来。
pizzas = ["assorted pizza","vegetable pizza","Margherita pizza","pepperoni pizza"]
for pizza in pizzas:
print(pizza)
assorted pizza
vegetable pizza
Margherita pizza
pepperoni pizza
4-1-1 修改这个for循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I lke pepperoni pizza”。
pizzas = ["assorted pizza","vegetable pizza","Margherita pizza","pepperoni pizza"]
for pizza in pizzas:
print("I like " + pizza + '.')
I like assorted pizza.
I like vegetable pizza.
I like Margherita pizza.
I like pepperoni pizza.
4-1-2 在程序末尾添加一行代码,它不在for循环中,指出你有多喜欢比萨。
pizzas = ["assorted pizza","vegetable pizza","Margherita pizza","pepperoni pizza"]
for pizza in pizzas:
print("I like " + pizza + '.')
print("I really love pizza!")
I like assorted pizza.
I like vegetable pizza.
I like Margherita pizza.
I like pepperoni pizza.
I really love pizza!
4-2 动物:想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for循环将每种动物的名称都打印出来。
animals = ["lion","tiger","leopard"]
for animal in animals:
print(animal)
lion
tiger
leopard
4-2-1 修改这个程序,使其针对每种动物都打印一个句子。
animals = ["lion","tiger","leopard"]
for animal in animals:
print("A " + animal + " would not make a great pet.")
A lion would not make a great pet.
A tiger would not make a great pet.
A leopard would not make a great pet.
4-2-2 在程序末尾添加一行代码,指出这些动物的共同之处。
animals = ["lion","tiger","leopard"]
for animal in animals:
print("A " + animal + " would not make a great pet.")
print("Any of these animals would not make a great pet!")
A lion would not make a great pet.
A tiger would not make a great pet.
A leopard would not make a great pet.
Any of these animals would not make a great pet!
4-3 数到20:使用一个for循环打印数字1~20。
for number in range(1,21):
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~1000000,再使用一个for循环将这些数字打印出来。
for number in range(1,1000001):
print(number)
4-5 计算1~1000000的总和。
numbers = range(1,1000001)
print(min(numbers))
print(max(numbers))
print(sum(numbers))
1
1000000
500000500000
Python几乎是瞬间就得到将一百万个数字相加的结果。4-6 奇数:创建一个包含在1~20内的奇数的列表
for number in range(1,21,2):
print(number)
1
3
5
7
9
11
13
15
17
19
4-7 3的倍数:创建一个包含在3~30内能被3整除的数字的列表。
for number in range(3,31,3):
print(number)
3
6
9
12
15
18
21
24
27
30
4-8 立方:创建一个包含前10个整数的立方的列表。
cubes = []
for number in range(1,11):
cubes.append(number ** 3)
for cube in cubes:
print(cube)
1
8
27
64
125
216
343
512
729
1000
4-9 立方解析:使用列表解析生成一个列表,其中包含前10个整数的立方。
cubes = [value ** 3 for value in range(1,11)]
for cube in cubes:
print(cube)
1
8
27
64
125
216
343
512
729
1000
4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码。
4-10-1 打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个元素。
cubes = [value ** 3 for value in range(1,11)]
print("The first three items in the list are:", end = ' ')
print(cubes[0:3])
The first three items in the list are: [1, 8, 27]
4-10-2 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素。
cubes = [value ** 3 for value in range(1,11)]
print("Three items from the middle of the list are:", end = ' ')
print(cubes[3:6])
Three items from the middle of the list are: [64, 125, 216]
4-10-3 打印消息“The last three items in list are:”,再使用切片来打印列表末尾的三个元素。
cubes = [value ** 3 for value in range(1,11)]
print("The last three items in list are:", end = ' ')
print(cubes[-3:])
The last three items in list are: [512, 729, 1000]
4-11 你的比萨和我的比萨:在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas中
4-11-1 在原来的比萨列表中添加一种比萨。
4-11-2 在列表friend_pizzas中添加另一种比萨。
4-11-3 核实你有两个不同的列表。
pizzas = ["assorted pizza","vegetable pizza","Margherita pizza","pepperoni pizza"]
friend_pizzas = pizzas[:]
pizzas.append("Hawaii pizza")
friend_pizzas.append("Mexico pizza")
print("My favorite pizzas are:")
for pizza in pizzas:
print(pizza)
print("\nMy friend's favorite pizzas are:")
for pizza in pizzas:
print(pizza)
My favorite pizzas are:
assorted pizza
vegetable pizza
Margherita pizza
pepperoni pizza
Hawaii pizza
My friend's favorite pizzas are:
assorted pizza
vegetable pizza
Margherita pizza
pepperoni pizza
Hawaii pizza
4-12 使用多个循环:在food.py中编写两个for循环,将各个食品列表都打印出来。
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
for food in my_foods:
print(food)
print("\nMy friend's favorite foods are:")
for food in friend_foods:
print(food)
My favorite foods are:
pizza
falafel
carrot cake
cannoli
My friend's favorite foods are:
pizza
falafel
carrot cake
ice cream
4-13 自助餐:有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
4-13-1 使用一个for循环将该餐馆提供的五种食品都打印出来。
foods = ("Steak","Roast Turkey","Pizza","Macaroni","Milk")
for food in foods:
print(food)
Steak
Roast Turkey
Pizza
Macaroni
Milk
4-13-2 尝试修改其中的一个元素,核实Python确实会拒绝你这样做。
foods = ("Steak","Roast Turkey","Pizza","Macaroni","Milk")
foods[0] = "Sour Cucumber Soup"
for food in foods:
print(food)
Traceback (most recent call last):
File "food.py", line 3, in <module>
foods[0] = "Sour Cucumber Soup"
TypeError: 'tuple' object does not support item assignment
4-13-3 餐馆调整了餐单,替换了它提供的其中两种食品。请给元组变量赋值,并使用一个for循环将新元组的每个元素都打印出来。
foods = ("Steak","Roast Turkey","Pizza","Macaroni","Milk")
foods = ("Apple Salad","Roast Turkey","Pizza","Macaroni","Sour Cucumber Soup")
for food in foods:
print(food)
Apple Salad
Roast Turkey
Pizza
Macaroni
Sour Cucumber Soup