python if多条件并列判断的三种方法

python if多条件并列判断的三种方法

如果使用python的if进行多个条件表达式的判断呢?下面介绍三种方法:

  1. 使用and或or来连接多个条件表达式,比如条件1 and 条件2 and条件3等等,当使用and连接多个表达式时,只要其中一个表示式为False,则if的条件为False,否则为True,相反,or连接的表达式中,只要有一个表达式为True,则if的条件为True,否则为False;
  2. and和or的混合使用,二者的优先级按前后顺序执行;
  3. 使用比较运算符,比如 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

全栈后端 / python教程 :


























Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.