例子一:最简单的for循环
# Author: Alex Chen for i in range(10): print(i)
例子二:加入步长
# Author: Alex Chen for i in range(0, 10, 2): print(i)
range(0, 10, 2) 中0表示从0开始,10表示到10结束,2表示步长为2,运行结果即是从0到10每个2为进行打印。
运行结果为:
0 2 4 6 8
例子三:循环嵌套
# Author: Alex Chen for i in range(10): print("for one:---------", i) for j in range(10): print("for two:", j)
例子四:加入break
# Author: Alex Chen for i in range(10): print("for one:---------", i) for j in range(10): if j > 5: break print("for two:", j)