EX32

 1 the_count = [1, 2, 3, 4, 5]
 2 fruits = ['apples', 'oranges', 'pears', 'apricots']
 3 change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
 4 
 5 # this first kind of for-loop goes through a list
 6 for number in the_count:
 7     print "This is count %d" % number
 8     
 9 # sanme as above
10 for fruit in fruits:
11     print "A fruit of type: %s" % fruit
12     
13 # also we can go through mixed lists too
14 # notic we have to use %r since we don't know what's in it
15 for i in change:
16     print "I go %r" % i
17     
18 # we can also build lists, first start with an empty one
19 elements = []
20 
21 # then use the range function to do 0 to 5 counts
22 for i in range(0 ,6):
23     print "Adding %d to the list." % i
24     # append is a function that lists understand
25     elements.append(i)
26     
27 # now we can print them out too
28 for i in elements:
29     print "Element was: %d" % i
30     

for:用来循环和打印各种各样的列表

elements.append():append向后面添加元素,参数可以是任何东西,将作为元素添加到列表尾部。

rang():如果你需要一个数值序列,使用内建函数range() 会很方便,它产生等差级数序列。

posted @ 2017-02-08 22:24  LevenLau  阅读(188)  评论(0编辑  收藏  举报