python if多条件并列判断的三种方法
·
python if多条件并列判断的三种方法
如果使用python的if进行多个条件表达式的判断呢?下面介绍三种方法:
- 使用and或or来连接多个条件表达式,比如条件1 and 条件2 and条件3等等,当使用and连接多个表达式时,只要其中一个表示式为False,则if的条件为False,否则为True,相反,or连接的表达式中,只要有一个表达式为True,则if的条件为True,否则为False;
- and和or的混合使用,二者的优先级按前后顺序执行;
- 使用比较运算符,比如 1<=x >=2;
if多条件并列判断实例代码
>>> if True and True and False:
... print("True")
...
>>> if False or False or True:
... print("True")
...
True
>>> if True and False or True:
... print("True")
...
True
>>> if True and False or False:
... print("True")
...
>>> if 1 < 2 < 3:
... print("True")
...
True
更多推荐
所有评论(0)