教材第四章练习题

时间:2023-02-22 21:21:18

4-1 比萨

pizzas = ['pepperoni pizza', 'beef pizza', 'chicken pizza']
for pizza in pizzas:
print(pizza)
for pizza in pizzas:
print("I like " + pizza + ".")
print("I really love pizza!")

输出

pepperoni pizzabeef pizzachicken pizzaI like pepperoni pizza.I like beef pizza.I like chicken pizza.I really love pizza!

4-2 动物

animals = ['dog', 'cat', 'monkey']for animal in animals:	print(animal)for animal in animals:	print("A " + animal + " would make a great pet.")print("Any of these animals would make a great pet!")

输出

dogcatmonkeyA dog would make a great pet.A cat would make a great pet.A monkey would make a great pet.Any of these animals would make a great pet!

4-3 数到20

for i in range(1,21):	print(i)
输出
1234567891011121314151617181920

4-5 计算1~1 000 000的总和

number =[]for i in range(1,1000001):	number.append(i)print("The minimum of list is " + str(min(number)))print("The maximum of list is " + str(max(number)))print("The sum of list is " + str(sum(number)))

输出

The minimum of list is 1The maximum of list is 1000000The sum of list is 500000500000[Finished in 0.3s]

4-6 奇数

numbers =[]for i in range(1,21,2):	numbers.append(i)for number in numbers:	print(number)

输出

135791113151719

4-7 3的倍数

numbers =[]for i in range(3,31,3):	numbers.append(i)for number in numbers:	print(number)

输出

36912151821242730

4-8 立方

numbers =[]for i in range(1,11):	numbers.append(i**3)for number in numbers:	print(number)

输出

1827641252163435127291000

4-9 立方解析

numbers =[value**3 for value in range(1,11)]for number in numbers:	print(number)

输出

1827641252163435127291000

4-10 切片

numbers =[value**3 for value in range(1,11)]for number in numbers:	print(number)print("The first three items in the list are: ")for value in numbers[:3]:	print(value)print("Three items from the middle of the list arez: ")for value in numbers[4:7]:	print(value)print("The last three items in the list are: ")for value in numbers[-3:]:	print(value)

输出

1827641252163435127291000The first three items in the list are: 1827Three items from the middle of the list arez: 125216343The last three items in the list are: 5127291000

4-11 你的比萨和我的比萨

pizzas = ['pepperoni pizza', 'beef pizza', 'chicken pizza']for pizza in pizzas:	print(pizza)for pizza in pizzas:	print("I like " + pizza + ".")print("I really love pizza!")friend_pizzas = pizzas[:]pizzas.append('fruit pizza')friend_pizzas.append('sea pizza')print("My favorite pizzas are:")for pizza in pizzas:	print(pizza)print("My friend\'s favorite pizzas are: ")for pizza in friend_pizzas:	print(pizza)

输出

pepperoni pizzabeef pizzachicken pizzaI like pepperoni pizza.I like beef pizza.I like chicken pizza.I really love pizza!My favorite pizzas are:pepperoni pizzabeef pizzachicken pizzafruit pizzaMy friend's favorite pizzas are: pepperoni pizzabeef pizzachicken pizzasea pizza

4-12 使用多个循环

my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:]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:pizzafalafelcarrot cakeMy friend's favorite foods are:pizzafalafelcarrot cake