码迷,mamicode.com
首页 > 其他好文 > 详细

EX32

时间:2017-02-08 23:15:48      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:class   序列   cti   style   技术   code   com   one   for   

 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() 会很方便,它产生等差级数序列。

EX32

标签:class   序列   cti   style   技术   code   com   one   for   

原文地址:http://www.cnblogs.com/LevenLau/p/6380131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!