python中逻辑运算符
Logical Operators:
逻辑运算符:
There are basically three types of logical operators as below:
基本上有以下三种类型的逻辑运算符:
- And
和
- Or
要么
- Not
不
There above operators semantics or meaning is the same as their meaning in English. To demonstrate we can take one simple example like a number can be greater then 2 and less then 5, so if we consider this example then we know that there could be only two possible numbers comes in between i.e. 3 and 4. So now let us run this example and verify the results by passing different numbers.
上面的运算符的语义或含义与英语中的含义相同。 为了说明这一点,我们可以举一个简单的例子,例如一个数字可以大于2,然后小于5,因此,如果考虑此示例,我们知道在3和4之间可能只有两个可能的数字。所以现在让我们运行此示例,并通过传递不同的数字来验证结果。
my_input = raw_input("Input any number less than 10 >>>")
print("Number entered was ") + my_input
my_input = int(my_input)
if (my_input > 2 and my_input < 5):
result = True
else:
result = False
print result
We have entered say 3 then the output of the above code will print as “True” because the condition satisfies that the passed number is greater than 2 and less than 3. The above code is a simple example of how to use “AND” logical operators in Python.
我们输入了3,则上述代码的输出将打印为“ True”,因为条件满足传递的数字大于2且小于3。以上代码是如何使用“ AND”逻辑的简单示例Python中的运算符。
Input any number less than 10 >>> 3
Number entered was 3 True
Now let us take similar example passing the same number as 3 but using “OR” condition stating can be less then 2 or less then 5 if any one of the condition matches then the result should be true.
现在让我们以类似的示例为例,该示例传递与3相同的数字,但是如果条件中的任何一个匹配,则使用“ OR”条件说明可以小于2或小于5,然后结果应该为true。
my_input = raw_input("Input any number less than 10 >>>")
print("Number entered was ") + my_input
my_input = int(my_input)
if (my_input < 2 or my_input < 5):
result = True
else:
result = False
print result
Below is the result when we passed “3” using OR logical operator to the conditions.
下面是当我们使用OR逻辑运算符将“ 3”传递给条件时的结果。
Input any number less than 10 >>> 3
Number entered was 3
True
And below if the False output as 6 does not fall under any criteria.
并且在以下条件下,如果False输出为6,则不属于任何条件。
Input any number less than 10 >>> 6
Number entered was 6
False
Now let us take an example on how to use “Not” logical operators. Say we have a result from some output and now our variable contains the value as 4 and let’s call the variable name as “abc”.
现在让我们举一个关于如何使用“非”逻辑运算符的例子。 假设我们从某个输出中得到了一个结果,现在我们的变量包含的值为4,我们将变量名称称为“ abc”。
Below is my condition to validate if the value from “abc” is present in the list?
以下是我要验证列表中是否存在“ abc”值的条件?
abc = 4
if abc not in [3,7,5] :
print("condition met")
else:
print("condition not met")
Result as below.
结果如下。
condition met
Now we know how to use logical operators in Python programming. This is a basic explanation on how to use Logical Operators in Python programming language.
现在我们知道了如何在Python编程中使用逻辑运算符。 这是有关如何在Python编程语言中使用逻辑运算符的基本说明。
Thank you for reading my article, please feel free to leave me some feedback or to suggest any future topics.
感谢您阅读我的文章,请随时给我一些反馈或提出任何未来的话题。
I’ll be looking forward to hearing from you – Swadhin Ray (Sloba)
我期待着您的回音-Swadhin Ray(Sloba)
For more information about me, please check out my Experts Exchange Profile page.
有关我的更多信息,请查看我的Experts Exchange个人资料页面。
Edited by: Andrew Leniart
编辑:安德鲁·列尼阿特(Andrew Leniart)
翻译自: https://www.experts-exchange.com/articles/32797/Logical-Operators-in-Python.html
python中逻辑运算符