Get the App
SLTechnology News&Howtos  ›  Development  › 

How does python evaluate multiple conditions

Shulou Source: shulou.com Published: 2022-06-01 05:12:42 10月03日 Update

This article is about how python evaluates multiple conditions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Evaluate multiple conditions

Usually, we need to evaluate multiple conditions. There are several possible options. For numeric values, we can compare the same variable multiple times. In this case, we can link to these comparisons.

# Multiple Comparisons # The typical way if a

< 4 and a >

1: # do something here# Do this instead if 1

< a < 4: # do somerthing here 在其他一些情况下,我们可以进行多个相等比较,并且可以使用以下in关键字进行成员测试。 # The typical way if b == "Mon" or b == "Wed" or b == "Fri" or b == "Sun": # do something here# Do this instead, you can also specify a tuple ("Mon", "Wed", "Fri", "Sun") if b in "Mon Wed Fri Sun".split(): # do something here 另一种技术是使用内置的all()和any()函数用于评估多个条件的功能。具体而言,该all()函数将评估何时迭代中的元素全部为True,因此该函数适合于替换一系列AND逻辑比较。另一方面,该any()函数的计算结果为True当迭代中的任何元素为True,因此适合替换一系列OR逻辑运算。相关示例如下所示。 # The typical ways if a < 10 and b >

5 and c = = 4: # do somethingif a

< 10 or b >

5 or c = 4: # do something# Do these instead if all ([a

< 10, b >

5, c = = 4]): # do somethingif any ([a

< 10, b >

5, c = = 4]): # do something Thank you for reading! This is the end of the article on "how python evaluates multiple conditions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Multiple evaluation condition function element content situation more article logic iteration good practical key keyword function variable on the other hand can make member Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple OPPO Reno Redmi Microsoft