Python编程:从入门到实践的动手试一试练习答案(第四章)

以下内容非标准答案,是我个人练习内容,仅供参考:

如有不准确,希望指出

# ~ #4-1
pizzas = ['培根pizza','大虾pizza','蘑菇pizza','菠萝pizza']
for pizza in pizzas :
	print(pizza + ' I like pepperoni pizza')
print(str(list(pizzas)) +'I really love pizza')
# ~ #4-2
# ~ #break这个命令网络上找的,如果不用,每次都会打印三遍
animals = ['lion','tiger','wolf']
for lion in animals:
	print('A lion at home, a mouse abroad.')
	break
for tiger in animals:
	print('A tiger lives off some small animals.')
	break
for wolf in animals:
	print('The wolf is a widely populated species in the world.')
	break
print('They all eat meat.')
# ~ #4-3
for value in range(1,21):
	print(value)
# ~ #4-4
# ~ #运行的时候注意,会很长
values = [value for value in range(1,1000001)]
print(values)
# ~ #4-5
values = list(range(1,1000001))
print(min(values))
print(max(values))
print(sum(values))
# ~ #4-6
values = range(1,20,2)
for i in values:
	print(i)
# ~ #4-7
values = range(3,31,3)
for i in values:
	print(i)
# ~ #4-8
squares = []
for value in range(1,11):
	squares.append(value**3)
print(squares)
# ~ #4-9 列表表达形式
squares = [value**3 for value in range(1,11)]
print(squares)
# ~ #4-10
squares = [value**3 for value in range(1,11)]
square_1 = squares[0:3]
square_2 = squares[3:6]
square_3 = squares[-3:]
print('The first three items in the list are:'+ str(square_1))
print('Three items from the middle of the list are:'+ str(square_2))
print('The last three items in the list are:'+ str(square_3))
# ~ #4-11
pizzas = ['培根pizza','大虾pizza','蘑菇pizza','菠萝pizza']
friend_pizzas = pizzas[:]
pizzas.append('蔬菜pizza')
friend_pizzas.append('海鲜pizza')
for pizza in pizzas:
	print('My favorite pizzas are :' + pizza)
for pizza in friend_pizzas:
	print("My friend's favorite pizzas are:" + pizza)
# ~ #4-12
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
for food in my_foods:
	print(food)
for food in friend_foods:
	print(food) 
# ~ #4-13
foods = ('pizza','apple','banana','cake','mango')
for food in foods:
	print(food)
foods = ('pizza','apple','ice cream','bread','mango')
for food in foods:
	print(food)

你可能感兴趣的:(Python编程:从入门到实际,动手试一试,答案)