PythonTip >> 博文 >> chinaunix

python函数与循环、变量的综合使用

zihua 2014-01-13 18:01:47 点击: 810 | 收藏


#! /usr/bin/python
i = 0
o = 7
p = 2
numbers = []

def w(startnum,endnum):
        while startnum < endnum:
                print "At the top is %d" % startnum
                numbers.append(startnum)
                startnum += p    #使用变量p
                print "Numbers now: ",numbers
                print "At the bottom i is %d" % startnum
w(i,o)        #函数w引用实参i,o
print "The numbers: "
for num in numbers:
        print num
'''-----------------------将代码改为用for-loop----------------'''
#! /usr/bin/python
i = 0
o = 6

numbers = []
for s in range(i,o):
        print "print i: %d " % s
        numbers.append(s)
        print "Numbers now: ", numbers

print "The numbers: "
for num in numbers:
    print num
'''----------range()在for-loop中的用法-----------'''
i = 1
o = 6
numbers = range(i,o)

for j in numbers:
    print "numbers was: %d" % j
原文链接:http://blog.chinaunix.net/uid-22521242-id-4008243.html

作者:zihua | 分类: chinaunix | 标签: python | 阅读: 810 | 发布于: 2014-01-13 18时 |