ex32.py

 1 # -*- coding:utf-8 -*-
 2 the_count = [1,2,3,4,5]
 3 fruits = ['appples', 'oranges', 'pears', 'apricots']
 4 change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
 5 
 6 #this frist kind of for-loop goes through a list
 7 for number in the_count:
 8     print ("This is count %d" % number)
 9 
10 # same as above
11 for fruit in fruits:
12     print ("A fruit of type: %s" % fruit)
13 
14 #also we can go through mixed lists too
15 # notice we have to use %r since we don't konw what's in it
16 for i in change:
17     print ("I got %r" % i)
18 
19 #we can also build lists,frist start with an empty one
20 elements = []  # 创建一个空列表
21 
22 #then use the range function to do 0 to 5 counts
23 for i in range(0, 6):
24     print ("Adding %d to the list." % i)
25     # append is a function that lists understand
26     elements.append(i)  #往空列表中追加元素
27     
28 # now we can print the out too
29 for i in elements:
30     print ("Element was: %d" % i)
31 
32 应该看到的结果:
33 """This is count 1
34 This is count 2
35 This is count 3
36 This is count 4
37 This is count 5
38 A fruit of type: appples
39 A fruit of type: oranges
40 A fruit of type: pears
41 A fruit of type: apricots
42 I got 1
43 I got 'pennies'
44 I got 2
45 I got 'dimes'
46 I got 3
47 I got 'quarters'
48 Adding 0 to the list.
49 Adding 1 to the list.
50 Adding 2 to the list.
51 Adding 3 to the list.
52 Adding 4 to the list.
53 Adding 5 to the list.
54 Element was: 0
55 Element was: 1
56 Element was: 2
57 Element was: 3
58 Element was: 4
59 Element was: 5"""

 

posted @ 2016-11-23 15:04  听风呤  阅读(118)  评论(0编辑  收藏  举报