表达式for loop
最简单的循环10次
1
2
3
4
5
6
|
#_*_coding:utf-8_*_ __author__ = 'Alex Li' for i in range ( 10 ): print ( "loop:" , i ) |
输出:
1
2
3
4
5
6
7
8
9
10
|
loop: 0 loop: 1 loop: 2 loop: 3 loop: 4 loop: 5 loop: 6 loop: 7 loop: 8 loop: 9 |
需求一:还是上面的程序,但是遇到小于5的循环次数就不走了,直接跳入下一次循环
1
2
3
4
|
for i in range ( 10 ): if i< 5 : continue #不往下走了,直接进入下一次loop print ( "loop:" , i ) |
需求二:还是上面的程序,但是遇到大于5的循环次数就不走了,直接退出
1
2
3
4
|
for i in range ( 10 ): if i> 5 : break #不往下走了,直接跳出整个loop print ( "loop:" , i ) |
for.py
#!/usr/bin/env python
for i in range(0,10,1): # range(start:int,stop:int,step:int)
if i<3:
print("loop",i)
else:
continue
print("hehe")