Learn python the hard way_习题32_循环和列表
2017-03-04 21:27
597 查看
the_count = [1, 3, 5, 6]
fruits = ['apple', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# this first kind of for-loop goes through a list.
for people in the_count:
print "This is people %d" % people
#same as above
for fruit in fruits:
print "A fruit of type: %s" % fruit
#also we ca go through mixed lists too
#notice we have to use %r since we don't know what's in it.
for x in change:
print "I got %r" % x
#we can also build lists, first start with an empty one
elements = []
#then use the range function to do 0 to 5 counts
for i in range(0, 6):
print "Adding %d to the list." % i
#append is a function that lists understand
elements.append(i)
#now we can print them out
for i in elements:
print "Element was:%d" % I
#note:
for X in list:
print "..........%s" %X
fruits = ['apple', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# this first kind of for-loop goes through a list.
for people in the_count:
print "This is people %d" % people
#same as above
for fruit in fruits:
print "A fruit of type: %s" % fruit
#also we ca go through mixed lists too
#notice we have to use %r since we don't know what's in it.
for x in change:
print "I got %r" % x
#we can also build lists, first start with an empty one
elements = []
#then use the range function to do 0 to 5 counts
for i in range(0, 6):
print "Adding %d to the list." % i
#append is a function that lists understand
elements.append(i)
#now we can print them out
for i in elements:
print "Element was:%d" % I
#note:
for X in list:
print "..........%s" %X
相关文章推荐
- Learn Python The Hard Way学习(32) - 循环和列表
- 列表的操作,习题38,learn python the hard way.
- Learn Python The Hard Way 习题41详解
- 【在路上】Learn Python The Hard Way 习题心得(ex16,17,18)
- Python 函数与变量--习题19,learn python the hard way
- 【Python】Learn Python the hard way, ex35 通过一个简单游戏,练习循环和分支语句
- learn the python in hard way习题16~20的附加习题
- 【Python】Learn Python the hard way, ex38 列表操作
- learn python the hard way(笨办法学python) 习题22
- 函数与文件-习题20--learn python the hard way
- 学习learn python the hard way习题46, ImportError问题
- 【在路上】Learn Python The Hard Way 习题心得(ex11,12,13,14,15)
- Learn Python The Hard Way-习题48 lexicon.py之我的实现
- learn python the hard way(笨办法学python) 练习32 附加练习
- Learn Python The Hard Way学习(33) - While循环
- Learn Python the hard way 习题49
- learn the python in hard way习题36~39的附加习题
- 【在路上】Learn Python The Hard Way 习题心得(ex8,9,10)
- Learn Python The Hard Way中习题46安装distribute包和virtualenv包
- Learn Python The Hard Way学习(34) - 访问列表中的元素