python入门指南:控制语句
Posted awildfish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python入门指南:控制语句相关的知识,希望对你有一定的参考价值。
条件控制
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
以上是关于python入门指南:控制语句的主要内容,如果未能解决你的问题,请参考以下文章
2018-06-20 中文代码示例视频演示Python入门教程第四章 控制流