4-1比萨
pizzas = ['chicken pizza', 'beef pizza', 'seafood pizza']
for pizza in pizzas:
print(pizza)
for pizza in pizzas:
print("I like {}".format(pizza))
print("I really love pizza")
4-2动物
pets = ['dog', 'cat', 'fish']
for pet in pets:
print("A {} would make a great pet".format(pet))
print("Any of these animals would make a great pet")
4-3数到20
for i in range(1,20):
print(i)
4-4一百万
li = range(1,1000000)
for dig in li:
print(li)
4-5计算1~1000000的总和
li = range(1, 1000000)
print(min(li))
print(max(li))
print(sum(li))
4-6奇数
li = range(1,20,2)
for dig in li:
print(dig)
4-7 3的倍数
li = range(3,30,3)
for dig in li:
print(dig)
4-8立方
4-9立方解析
li = [value**3 for value in range(1,10)]
for dig in li:
print(dig)
4-10切片
li = range(1, 20, 2)
print("The first three items in the list are:")
for dig in li[0,3]:
print(dig)
print("Three items from the middle of the list are:")
for dig in li[1,4]:
print(dig)
print("The Last three items in the list are:")
for dig in li[-3:]:
print(dig)
4-11你的比萨和我的比萨
pizzas = ['chicken pizza', 'beef pizza', 'seafood pizza']
fpizzas = pizzas
fpizzas.append('mashroom pizza')
print('my favorite pizza is:')
for pizza in pizzas:
print(pizza)
print('my friends\' favorite pizza is:')
for pizza in fpizzas:
print(pizza)