i = 0
numbers = []
while i < 6:
print "At the top i is %d" % i
numbers.append(i)
i += 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
"""
Test Result:
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
"""【Python】Learn Python the hard way, ex33 while循环
最新推荐文章于 2025-07-10 15:31:34 发布
本文通过一个简单的Python脚本示例介绍了如何使用while循环来构建一个数字列表,并展示了循环过程中变量的变化情况及最终形成的列表内容。
540

被折叠的 条评论
为什么被折叠?



