在Python中有三种控制流语句——if、for和while。
1、if语句块:
if 条件<a href="https://www.gaodaima.com/tag/%e8%a1%a8%e8%be%be%e5%bc%8f" title="查看更多关于表达式的文章" target="_blank">表达式</a>1: 当条件1为<a href="https://www.gaodaima.com/tag/%e7%9c%9f%e6%97%b6" title="查看更多关于真时的文章" target="_blank">真时</a>你要执行的代码 elif 条件表达式2: 当条件2为真时你要执行的代码 else: 上述两条件都不满足时执行的代码
www#gaodaima.com来源gaodai$ma#com搞$代*码*网搞代码
注:elif部份和else部份是可有可无的,视你的需要而定。以上的某个条件表达式为真时,则执行其相应的语句块,其后的条件表达式则忽略。
例1:
if True: print ('Yes, it is true')
输出:
Yes, it is true
说明:True代表条件为真,它是布尔值。print语句块始终执行。非零的数都表示真;0为假,既条件不成立。
推荐学习《python教程》。
来源:搞代码网:原文地址:https://www.gaodaima.com