条件和条件语句
1.布尔值
下列值在作为布尔表达式时,会被解释器看作假(false
):
False None 0 "" () [] {}
True == 1 # True
False == 0 # True
True + False + 42 # 43
bool函数可以转换其他类型值为布尔值;
2.if-else语句
if condition:
if condition is true
else:
if condition is false
3.elif语句
if condition1:
if condition1 is true
elif condition2:
if condition2 is true
else:
if they are both false
4.更复杂的条件
表达式 | 描述 |
---|---|
x is y | x和y是同一个对象 |
x is not y | x和y是不同的对象 |
x in y | x是y容器的成员 |
x not in y | x不是y容器的成员 |