Python流程控制 if / for/ while
在Python中没有switch语句
If语句
if condition:
do sth
elif condition:
Do sth
else:
Do sth
while语句有一个可选的else从句
while condition:
do sth
else:
do sth
for循环
for i in range(1, 5): # 即序列[1, 2, 3, 4]
print i
else:
print 'The for loop is over'
break语句
如果你从for或while循环中 终止 ,任何对应的循环else块将不执行。
try..except语句处理异常
import sys
try:
s = raw_input('Enter something --> ')
except EOFError:
print '\nWhy did you do an EOF on me?'
sys.exit() # exit the program
except:
print '\nSome error/exception occurred.'
# here, we are not exiting the program
print 'Done'
可以使用raise语句 引发 异常,相当于java中的throw