《Python从⼊门到实践第⼆版》第四章练习
4-1 ⽐萨 想出⾄少三种你喜欢的⽐萨,将其名称存储在⼀个列表中,再使⽤for 循环将每种⽐萨的名称打印出来。
# 修改这个for 循环,使其打印包含⽐萨名称的句⼦,⽽不仅仅是⽐萨的名称。对于每种⽐萨,都显⽰⼀⾏输出,下⾯是⼀个例⼦。
# I like pepperoni pizza.
pizzas =['a','b','c']
for pizza in pizzas:
print('I like pepperoni '+ pizza +'.')
# 在程序末尾添加⼀⾏代码,它不在for 循环中,指出你有多喜欢⽐萨。输出应包含针对每种⽐萨的消息,还有⼀个总结性句⼦,下⾯是⼀个例⼦。
# I really love pizza!
print('I really love pizza!')
4-2 动物 想出⾄少三种有共同特征的动物,将其名称存储在⼀个列表中,再使⽤for 循环将每种动物的名称打印出来。
animals =['cow','sheep','horse']
python新手代码练习for animal in animals:
print(animal)
# 修改这个程序,使其针对每种动物都打印⼀个句⼦,下⾯是⼀个例⼦。
# A dog would make a great pet.
print('A '+ animal +' would make a great pet.')
# 在程序末尾添加⼀⾏代码,指出这些动物的共同之处,如打印下⾯这样的句⼦。
# Any of these animals would make a great pet!
print('Any of these animals would make a great pet!')
4-3 数到20 使⽤⼀个for 循环打印数1~20(含)。
for i in range(1,21):
print(i)
4-4 ⼀百万 创建⼀个包含数1~1 000 000的列表,再使⽤⼀个for 循环将这些数打印出来。(如果输出的时间太长, 按Ctrl + C停⽌输出或关闭输出窗⼝。)
num =list(range(1,1000001))
for i in num:
print(i)
4-5 ⼀百万求和 创建⼀个包含数1~1 000 000的列表,再使⽤min() 和max() 核实该列表确实是从1开始到1000000结束的。另外,对这个列表调⽤函数sum() ,看看Python将⼀百万个数相加需要多长时间。
num =list(range(1,1000001))
print(min(num))
print(max(num))
print(sum(num))
4-6 奇数 通过给函数range() 指定第三个参数来创建⼀个列表,其中包含1~20的奇数,再使⽤⼀个for 循环将这些数打印出来。
odds =list(range(1,21,2))
for odd in odds:
print(odd)
4-7 3的倍数 创建⼀个列表,其中包含3~30能被3整除的数,再使⽤⼀个for 循环将这个列表中的数打印出来。
# num = list(range(3,31,3))
for i in(list(range(3,31,3))):
print(i)
4-8 ⽴⽅ 将同⼀个数乘三次称为⽴⽅ 。例如,在Python中,2的⽴⽅⽤2**3 表⽰。请创建⼀个列表,其中包含前10个整数(1~10)的⽴⽅,再使⽤⼀个for 循环将这些⽴⽅数打印出来。
numb =[]
for num in range(1,11):
nnn = num **3
numb.append(nnn)
print(numb)
4-9 ⽴⽅解析 使⽤列表解析⽣成⼀个列表,其中包含前10个整数的⽴⽅。
list1 =[num **3for num in range(1,11)]
print(list1)
4-10 切⽚ 选择你在本章编写的⼀个程序,在末尾添加⼏⾏代码,以完成如下任务。
pets =['cat','dog','fish','horse','sheep']
# 打印消息“The first three items in the list are:”,再使⽤切⽚来打印列表的前三个元素。
print('The first three items in the list are:')
print(pets[:3])
# 打印消息“Three items from the middle of the list are:”,再使⽤切⽚来打印列表的中间三个元素。
print('Three items from the middle of the list are:')
print(pets[1:4])
# 打印消息“The last three items in the list are:”,再使⽤切⽚来打印列表的末尾三个元素。
print('The last three items in the list are:')
print(pets[-3:])
4-11 你的⽐萨,我的⽐萨 在你为完成练习4-1⽽编写的程序中,创建⽐萨列表的副本,并将其赋给变量friend_pizzas ,再完成如下任务。
my_pizzas =['a','b','c']
friend_pizzas = my_pizzas[:]
# 在原来的⽐萨列表中添加⼀种⽐萨。
my_pizzas.append('e')
# 在列表friend_pizzas 中添加另⼀种⽐萨。
friend_pizzas.append('g')
# 核实有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使⽤⼀个for 循环来打印第⼀个列表;打印消息“My friend's favorite pizzas are:”,再使⽤⼀个for循环来打印第⼆个列表。核实新增的⽐萨被添加到了正确的列表中。
print('My favorite pizzas are:')
for my_pizza in my_pizzas:
print(my_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 循环,将各个⾷品列表打印出来。
my_foods =['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
for my_food in my_foods:
print(my_food)
friend_foods.append('ice cream')
for friend_food in friend_foods:
print(friend_food)
4-13 ⾃助餐 有⼀家⾃助式餐馆,只提供五种简单的⾷品。请想出五种简单的⾷品,并将其存储在⼀个元组中。
foods =('hamburger','chips','coffee','tee','sushi')
# 使⽤⼀个for循环将该餐馆提供的五种⾷品都打印出来。尝试修改其中的⼀个元素,核实Python确实会拒绝你这样做。
for food in foods:
print(food)
foods[1]='mike'
# 餐馆调整了菜单,替换了它提供的其中两种⾷品。请编写⼀个这样的代码块:给元组变量赋值,并使⽤⼀个for 循环将新元组的每个元素都打印出来foods =('bread','noddles','coffee','tee','sushi')
for food in foods:
print(food)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。