# 4-1 比萨
pizzas = ['seafood', 'sausage', 'cheese']
for i in pizzas:
print(i)
for i in pizzas:
print('I like ' + i + ' pizza') # 4-1-1
for i in pizzas:
print('I like ' + i + ' pizza')
print('I really love pizza!') # 4-1-2
# 4-3 数到20
for i in range(1, 21):
print(i)
# 4-4 一百万
million = list(range(1, 1000001))
for i in million:
print(i)
# 4-5 计算1~1 000 000 的总和
print(min(million))
print(max(million))
sum(million) # 很快
# 4-6 奇数
nums = list(range(1, 20, 2))
for i in nums:
print(i)
# 4-7 3 的倍数
nums_3 = list(range(3, 31, 3))
for i in nums_3:
print(i)
# 4-8 立方
nums_3xx = []
for i in range(1, 11):
cube = i ** 3
nums_3xx.append(cube)
for i in nums_3xx:
print(i)
# 4-9 立方解析
nums_3xxx = [x ** 3 for x in range(1, 11)]
# 4-10 切片
my_foods = ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
print("The first three items in the list are:")
print(my_foods[:3]) # 4-10-1
print("\nThree items from the middle of the list are:")
print(my_foods[1:4]) # 4-10-2
print("\nThree items from the middle of the list are:")
print(my_foods[-3:]) # 4-10-3
# 4-11 你的比萨和我的比萨
my_foods = ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
friend_pizzas = my_foods[:]
my_foods.append('cheese') # 4-11-1
friend_pizzas.append('seafood') # 4-11-2
for i in my_foods:
print("My favorite pizzas are: " + i)
for i in friend_pizzas:
print("My friend’s favorite pizzas are: " + i)
# 4-13 自助餐
foods = ('辣椒', '娃娃菜', '毛肚', '鸭爪', '牛排')
for food in foods:
print(food) # 4-13-1
food[0] = '小龙虾' # 4-13-2
foods = foods[:3] + ('甲鱼', '猪蹄')
for food in foods:
print(food)