代码如下:
1 fruits = ['apples', 'oranges', 'pears', 'apricots'] 2 change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] 3 4 # this first kind of for-loop goes through a list 5 for number in the_count: 6 print "This is count %d" % number 7 8 # same as above 9 for fruit in fruits:10 print "A fruit of type: %s" % fruit11 12 # also we can go through mixed lists too13 # notice we have to use %r since we don't know what's in it14 for i in change:15 print "I got %r" % i16 17 # we can also build lists, first start with an empty one18 elements = []19 20 # then use the range function to do 0 to 5 counts21 for i in range(0, 6):22 print "Adding %d to the list." % i23 # append is a function that lists understand24 elements.append(i)25 26 # now we can print them out too27 for i in elements:28 print "Element was: %d" % i
运行结果: