第4章 操作列表

4-1比萨:

1 #4-1
2 pizzas = ['Pan Pizza','Thick style','Chicago Style']
3 for pizza in pizzas:
4     print('I like '+pizza)
5 print('I really love pizza')

4-2动物:

1 #4-2
2 pets = ['dog','cat','bird']
3 for pet in pets:
4     print('A '+pet+' would like make a great pet')
5 print('Any of thesee animals would a great pet!')

4-3数到20:

1 for value in range(1,21):
2     print(value)

4-4一百万:

1 #4-4
2 for value in range(1,100000):
3     print(value)

4-5计算1-1000000的总和:

1 #4-5
2 values = range(1,1000001)
3 print(min(values))
4 print(max(values))
5 print(sum(values))

4-6奇数:

1 #4-6
2 lists = [list for list in range(1,21,2)]
3 print(lists)

4-7 3的倍数:

1 #4-7
2 times = [list for list in range(3,31,3)]
3 print(times)

4-8 立方:

1 #4-8:
2 num = [value**3 for value in range(1,11)]
3 print(num)

4-9立方解析:

1 #4-9
2 list = [value ** 3 for value in range(1, 11)]
3 for num in list:
4     print (num)

4-10切片:

1 #4-10
2 list = [value  for value in range(1, 10)]
3 print(list)
4 print ("The first three items in the list are: ")
5 print (list[0:3])
6 print ("Three items from the middle of the list are: ")
7 print(list[3:-3])
8 print('The last three items in the list are:')
9 print (list[-3:])

4-11 你的比萨和我的比萨:

 1 #4-11
 2 pizzas = ['Pan Pizza','Thick style','Chicago Style']
 3 friend_pizzas = pizzas[:]
 4 pizzas.append('Seafood pizza')
 5 friend_pizzas.append('Beef pizza')
 6 print('My favorite pizza are:',end='')
 7 for pizza in pizzas:
 8     print(pizza+',',end='')
 9 print('\n')
10 print("My friend's favorite pizza are:",end='')
11 for friend_pizza in friend_pizzas:
12     print(friend_pizza+',',end='')

4-12使用多个循环:

1 my_foods = ["pizza", "falafel", "carrot cake"]
2 friend_foods = my_foods[:]
3 for food in my_foods:
4     print (food + " ", end="")
5 print ("")
6 for food in friend_foods:
7     print (food + " ", end="")
8 print ("")

 

posted @ 2020-05-07 07:34  猫山思  阅读(170)  评论(0)    收藏  举报