while 循环,它所作的和 if 语句类似,也是去检查一个布尔表达式的真假,不一
样的是它下面的代码片段不是只被执行一次,而是执行完后再调回到 while 所在的位置,
如此重复进行,直到 while 表达式为 False 为止
#!/bin/usr/python
i=0
numbers=[]
while i<5:
print "At the top i is %d" % i
numbers.append(i)
i = i+1
print "Number now:",numbers
print "At hte bottom i is %d" % i
print "The numbers:"
for num in numbers:
print num
打印输出结果
用for循环打印出同样的结果
#!/bin/usr/python
numbers=[]
for i in range(0,5):
numbers.append(i)
print "At the top i is %d" %(i)
print "Numbers now",numbers
print "At the botton i is %d" %(i+1)
输出结果
转载于:https://blog.51cto.com/5471815/1384557