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