条件控制
if,if-else,if-elseif-else
#!/bin/python a = 'test' if a == 'test': print('a is %s' %(a)) else: print('a is not test') if a == 'test': print('a is test') a = 'other' if a == 'test': print('a is test') elif a == 'test1': print('a is test1') else: print('a is not test or test1')
输出结果:
a is test a is test a is not test or test1
for循环控制
for循环中,加入控制流程:for-continue(继续循环),for-break(跳出循环)
#!/bin/python for i in range(1, 3): print(i) for i in [1, 2, 3]: print(i) for i in range(0, 100): if i <= 20: continue else: if i >= 25: break else: print(i)
输出结果:
1 2 1 2 3 21 22 23 24
while循环控制
#!/bin/python i = 0 while i <= 20: if i < 10: i += 1 continue; elif i >= 10 and i <= 15: i += 1 elif i == 16: break else: i += 1 print(i)
输出结果:
11 12 13 14 15 16