pizzas = ['海鲜比萨', '芝士比萨', '双拼比萨']
for pizza in pizzas: print(pizza)
for pizza in pizzas: print('我喜欢' + pizza)
numbers = [digit for digit in range(1, 1000001)]
for number in numbers:
print(number)
print(min(numbers))
print(max(numbers))
print(sum(numbers))
4-6 奇数:通过给函数 range()指定第三个参数来创建一个列表,其中包含 1~20的 奇数;再使用一个 for 循环将这些数字都打印出来。
odds = [digit for digit in range(1, 21, 2)]
for odd in odds:
print(odd)
4-7 3的倍数:创建一个列表,其中包含 3~30内能被 3整除的数字;再使用一个 for 循环将这个列表中的数字都打印出来。
factor_of_three = [digit*3 for digit in range(1, 11)]
for number in factor_of_three:
print(number)
#使用4-7的列表
print(
'the first three items in the list are:'
+ str(factor_of_three[0:3]))
mid = int(len(factor_of_three)/2)
print(
'Three items from the middle of the list are:'
+ str(factor_of_three[mid-1 : mid+2]))
print(
'The last three items in the list are:'
+ str(factor_of_three[-3:]))
4-13 自助餐:有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食 品,并将其存储在一个元组中。
foods = ('鸡肉', '鸭肉', '鱼肉', '猪肉', '牛肉')
for food in foods:
print(food)
print()
foods[1] = '羊肉'
foods = ('鸡肉', '羊肉', '鱼肉', '猪肉', '香肠')
for food in foods:
print(food)