条件语句:通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
其程序流程图如下:
条件语句包括:
if 判断条件:
执行语句……
else:
执行语句……
or
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
elif 判断条件3:
执行语句3……
else:
执行语句4……
示例:
str = 'world'
if str == 'world': # 判断变量是否为 world
print ('你好' )
else:
print (str) # 条件不成立时输出变量名称
if str == 'python': # 判断变量是否为 python
print ('hello' )
else:
print (str) # 条件不成立时输出变量名称