如何在python中处理if语句和and运算符?
在这个if语句中,这些变量instanceType、operatingSystem和Renancy都是用户输入,如何处理,不检查任何用户输入是否为None e、 g.如果instanceType为None,我想检查如何在python中处理if语句和and运算符?,python,python-2.7,if-statement,nested,Python,Python 2.7,If Statement,Nested,在这个if语句中,这些变量instanceType、operatingSystem和Renancy都是用户输入,如何处理,不检查任何用户输入是否为None e、 g.如果instanceType为None,我想检查 if instanceType == instance_type and operatingSystem == operating_system and tenancy == tenancy_db: sku_list.append(key) if operatingSyst
if instanceType == instance_type and operatingSystem == operating_system and tenancy == tenancy_db:
sku_list.append(key)
if operatingSystem == operating_system and tenancy == tenancy_db:
sku_list.append(key)
e、 g.如果操作系统为无,我想检查
if instanceType == instance_type and operatingSystem == operating_system and tenancy == tenancy_db:
sku_list.append(key)
if operatingSystem == operating_system and tenancy == tenancy_db:
sku_list.append(key)
e、 g.如果租约和instanceType都为None,我想检查:
if instanceType == instance_type and tenancy == tenancy_db:
sku_list.append(key)
同样,这取决于用户输入的是None还是其他方式,是否有其他方式可以做到这一点,或者我必须实现嵌套if-else?一种方式是声明一个helper函数:
if operatingSystem == operating_system:
sku_list.append(key)
一种方法是声明辅助函数:
if operatingSystem == operating_system:
sku_list.append(key)
您还可以使用
或
运算符,将None
视为false:
equal_or_none = lambda x, y: x is None or x == y
if (
equal_or_none(instanceType, instance_type)
and equal_or_none(operatingSystem, operating_system)
and equal_or_none(tenancy, tenancy_db)):
sku_list.append(key)
您还可以使用
或
运算符,将None
视为false:
equal_or_none = lambda x, y: x is None or x == y
if (
equal_or_none(instanceType, instance_type)
and equal_or_none(operatingSystem, operating_system)
and equal_or_none(tenancy, tenancy_db)):
sku_list.append(key)