For-loops 的两种方式:
1. the_count = [1, 2, 3, 4, 5]
for number in the_count:
print "This is count %d" % number
输出结果为:
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
2. for i in range(0, 6):
print i
输出结果为:
0
1
2
3
4
5
注意6是不执行的,也就是说是0 <= i < 6
while-loops:
A while-loop will keep executing the code block under it as long as a boolean expression is True.
Example:
i = 0
numbers = []
while i < 6:
print "At the top i is %d" % i
numbers.append(i)
i = i + 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
中途退出(适用于循环或判断):
exit(0)
但是使用之前要加上from sys import exit
while-loops:
A while-loop will keep executing the code block under it as long as a boolean expression is True.
Example:
i = 0
numbers = []
while i < 6:
print "At the top i is %d" % i
numbers.append(i)
i = i + 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
中途退出(适用于循环或判断):
exit(0)
但是使用之前要加上from sys import exit
没有评论:
发表评论