本文共 499 字,大约阅读时间需要 1 分钟。
python
中条件语句的三种结构:# if structureif True: print("True")
# if-else structureif True: print("True")else: print("False")
# if-elif-else structure if True: print("True")elif True: print("Still True")else: print("False")
但是,实际上当我们使用if-elif-else
语句结构时, elif
等同于else: if
, 如下所示,因此在elif
后面紧跟的else
语句可以去掉,并不一定要出现,因为if
语句中存在if
的单独判断结构。
elif condition:else:else: if condition: # else:
比如我们需要对变量a
进行赋值,想要在b
大于5
的时令a=5
,在b<=6
的时候令a=0
。
# 传统写法if b > 5: a = 5else: a = 0# 特殊写法a = 5 if b > 5 else 0
转载地址:http://opuh.baihongyu.com/