当前位置:   article > 正文

Python编程:从入门到实践第二版答案(第四章)_python快速编程入门第二版第四章课后题答案

python快速编程入门第二版第四章课后题答案

4-1

  1. pizzas = ['beef', 'meet', 'mutton']
  2. for pizza in pizzas:
  3. print(pizza)
  4. for pizza in pizzas:
  5. print(f"I like {pizza} pizza.")
  6. print("I really love pizza!")

 4-2

  1. animals = ['dog', 'cat', 'snake']
  2. for animal in animals:
  3. print(f"A {animal} would make a great pet.")
  4. print("Any of these animals would make a great pet!")
 

 

4-3

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

4-4

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

4-5

  1. numbers = list(range(1, 1_000_001))
  2. print(min(numbers))
  3. print(max(numbers))
  4. print(sum(numbers))

4-6

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

4-7

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

4-8

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

4-9

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

   4-10

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

4-11

  1. pizzas = ['beef', 'meet', 'mutton']
  2. friend_pizzas = pizzas[:]
  3. pizzas.append('banana')
  4. friend_pizzas.append('apple')
  5. print("My favorite pizzas are:")
  6. for pizza in pizzas:
  7. print(pizza)
  8. print("\nMy friend's favorite pizzas are:")
  9. for pizza in friend_pizzas:
  10. print(pizza)

4-12

  1. my_foods = ['pizza', 'falafel', 'carrot cake']
  2. friend_foods = my_foods[:]
  3. print("My favorite foods are:")
  4. for my_food in my_foods:
  5. print(my_food)
  6. print("\nMy friend's favorite foods are:")
  7. for friend_food in friend_foods:
  8. print(friend_food)

4-13

  1. foods = ('pizza', 'falafel', 'carrot cake', 'apple', 'button')
  2. for food in foods:
  3. print(food)
  4. foods = ('pizza', 'falafel', 'carrot cake', 'milk', 'banana')
  5. for food in foods:
  6. print(food)
  7. del foods[0]
  8. #报错
  9. Traceback (most recent call last):
  10. File "C:/Users/day/PycharmProjects/untitled1/数字", line 8, in <module>
  11. del foods[0]
  12. TypeError: 'tuple' object doesn't support item deletion

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/402516
推荐阅读
相关标签
  

闽ICP备14008679号