教材第四章练习题

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 pizza
beef pizza
chicken pizza
I 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!")

输出

dog
cat
monkey
A 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)
输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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 1
The maximum of list is 1000000
The 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)

输出

1
3
5
7
9
11
13
15
17
19

4-7 3的倍数

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

输出

3
6
9
12
15
18
21
24
27
30

4-8 立方

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

输出

1
8
27
64
125
216
343
512
729
1000

4-9 立方解析

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

输出

1
8
27
64
125
216
343
512
729
1000

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)

输出

1
8
27
64
125
216
343
512
729
1000
The first three items in the list are: 
1
8
27
Three items from the middle of the list arez: 
125
216
343
The last three items in the list are: 
512
729
1000

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 pizza
beef pizza
chicken pizza
I like pepperoni pizza.
I like beef pizza.
I like chicken pizza.
I really love pizza!
My favorite pizzas are:
pepperoni pizza
beef pizza
chicken pizza
fruit pizza
My friend's favorite pizzas are: 
pepperoni pizza
beef pizza
chicken pizza
sea 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:
pizza
falafel
carrot cake

My friend's favorite foods are:
pizza
falafel
carrot cake





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值