"""这是第四章的练习题"""
"""4-1"""
pizzas = ["red","big","blnk","hit","anger","top","aaa","bbbb"]#定义一个列表
mes = "I like pepperoni pizza"#定义一个消息字符串
for pizza in pizzas:#for循环取列表中的值
print(pizza + " , " + mes + ".\n")
print("I really love pizza!")
"""4-2"""
animals = ["dog","cat","pig"]
for animal in animals:
print("a" + " "+ animal + " would make a great pet")
print("Any of these animals would make a preat pet")
"""4-3"""
num = range(1,21)
for i in num:
print(i)
"""4-4,4-5"""
numa = list(range(1,1000001))#定义一个列表,从1到1000001
for i in numa:
print(i)
print(min(numa))#求列表中最小值
print(max(numa))#求列表中最大值
print(sum(numa))#求列表和
"""4-6"""
numb = list(range(1,21,2))#定义一个列表,从1到21中其中步长为2的数字(奇数)
for i in numb:
print(i)
"""4-7"""
numc = []#定义一个空的列表
temp = range(1,11)
for i in temp:
numc.append(i**3)#用append方法把值写入numc的列表中
for x in numc:#循环取值
print(x)
"""4-9"""
numd=[numd**3 for numd in range(1,11)]#使用列表解析生产一个列表
print(numd)
"""4-10"""
print("The first three items in the list are:")
print(pizzas[0:3])#切片列表中0-3元素
print("three items from the midddle of the list are:")
print(pizzas[3:6])
print("the last from the midddle of the list are:")
print(pizzas[-3:])#切片列表最后3个元素
"""4-11"""
friend_pizzas = pizzas[:]#复制列表中全部元素到新的列表中
print(friend_pizzas)
pizzas.append("tttt")
friend_pizzas.append("anchaj")
print("my favorite pizzas are:")
for i in pizzas:
print(i)
print("my friend's favorite pizzas are:")
for i in friend_pizzas:
print(i)
"""4-12"""
my_foofs = ["pizza","falafel","carrot cake "]
f_foods = my_foofs[:]
for x in my_foofs:
print(x)
for y in f_foods:
print(y)
"""4-13"""
foofs=('面条','米饭','饮料','咖啡','糕点')#这是一个元组用()切记不能用【】,【】代表列表,元组内的元素生命周期不可修改
print("今天的产品有:")
for i in foofs:
print(i)
print("\n")
foofs = ('西餐','中餐','饮料','咖啡','糕点')#修改元组
for i in foofs:
print(i)