for var in ....
statement
statement
else:
statement
例子:注意range(5)是5次循环,range(0,5)是5次循环,range(1,5)是4次循环,
for i in range(5):
print (i)
else:
print ("the for loop")
结果:
0
1
2
3
4
the for loop
例子:使用time模块,休息一秒
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
myD = {1: 'a', 2: 'b'}
for key, value in dict.items(myD):
print key, value
time.sleep(1) # 暂停 1 秒