Python编程:从入门到实践第二版答案(第四章)

4-1

pizzas = ['beef', 'meet', 'mutton']
for pizza in pizzas:
    print(pizza)

for pizza in pizzas:
    print(f"I like {pizza} pizza.")

print("I really love pizza!")

 4-2

animals = ['dog', 'cat', 'snake']
for animal in animals:
    print(f"A {animal} would make a great pet.")

print("Any of these animals would make a great pet!")
 

 

4-3

for value in range(1, 21):
    print(value)

4-4

numbers = list(range(1, 1_000_001))
for number in numbers:
    print(number)

4-5

numbers = list(range(1, 1_000_001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))

4-6

numbers = list(range(1, 20, 2))
for number in numbers:
    print(number)

4-7

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

4-8

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

4-9

numbers = [num**3 for num in range(1, 11)]
print(numbers)

   4-10

numbers = list(range(1, 20, 2))
print("The first three items in the list are:")
for num in numbers[:3]:
    print(num)
print("The items from the middle of the list are:")
for num in numbers[4:7]:
    print(num)
print("The last three items in the list are:")
for num in numbers[-3:]:
    print(num)

4-11

pizzas = ['beef', 'meet', 'mutton']
friend_pizzas = pizzas[:]
pizzas.append('banana')
friend_pizzas.append('apple')
print("My favorite pizzas are:")
for pizza in pizzas:
    print(pizza)
print("\nMy friend's favorite pizzas are:")
for pizza in friend_pizzas:
    print(pizza)

4-12

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]

print("My favorite foods are:")
for my_food in my_foods:
    print(my_food)
print("\nMy friend's favorite foods are:")
for friend_food in friend_foods:
    print(friend_food)

4-13

foods = ('pizza', 'falafel', 'carrot cake', 'apple', 'button')
for food in foods:
    print(food)
foods = ('pizza', 'falafel', 'carrot cake', 'milk', 'banana')
for food in foods:
    print(food)
del foods[0]

#报错
Traceback (most recent call last):
  File "C:/Users/day/PycharmProjects/untitled1/数字", line 8, in <module>
    del foods[0]
TypeError: 'tuple' object doesn't support item deletion

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机务猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值