Python 简明教程
Python - Logical Operators
Python Logical Operators
Python 逻辑运算符用于形成复合布尔表达式。这些逻辑运算符的每个操作数本身都是布尔表达式。例如,
Python logical operators are used to form compound Boolean expressions. Each operand for these logical operators is itself a Boolean expression. For example,
Example
age > 16 and marks > 80
percentage < 50 or attendance < 75
除了关键字 False,Python 将所有类型的数字零、空序列( strings 、 tuples 、 lists )、空 dictionaries 和空 sets 解释为 False。所有其他值都视为 True。
Along with the keyword False, Python interprets None, numeric zero of all types, and empty sequences (strings, tuples, lists), empty dictionaries, and empty sets as False. All other values are treated as True.
Python 中有三个逻辑运算符。它们是 “ and ”、“ or ”和“ not ”。它们必须是小写。
There are three logical operators in Python. They are "and", "or" and "not". They must be in lowercase.
Logical "and" Operator
对于复合布尔表达式为 True,两个操作数都必须为 True。如果任何或两个操作数的值为 False,则表达式返回 False。
For the compound Boolean expression to be True, both the operands must be True. If any or both operands evaluate to False, the expression returns False.
Logical "or" Operator
相反,如果任何一个操作数为 True,则或运算符返回 True。对于复合布尔表达式为 False,两个操作数都必须为 False。
In contrast, the or operator returns True if any of the operands is True. For the compound Boolean expression to be False, both the operands have to be False.
Logical "not" Operator
这是一个一元运算符。会颠倒其后的布尔操作数的状态。因此,not True 变成 False,not False 变成 True。
This is a unary operator. The state of Boolean operand that follows, is reversed. As a result, not True becomes False and not False becomes True.
How the Python interpreter evaluates the logical operators?
表达式 "x and y" 首先评估 "x"。若 "x" 为 false,则返回其值;否则,评估 "y" 并返回所产生的值。
The expression "x and y" first evaluates "x". If "x" is false, its value is returned; otherwise, "y" is evaluated and the resulting value is returned.
表达式 "x or y" 首先评估 "x";若 "x" 为 true,则返回其值;否则,评估 "y" 并返回所产生的值。
The expression "x or y" first evaluates "x"; if "x" is true, its value is returned; otherwise, "y" is evaluated and the resulting value is returned.
Python Logical Operators Examples
下面给出逻辑运算符的几个用例 -
Some use cases of logical operators are given below −
Example 1: Logical Operators With Boolean Conditions
x = 10
y = 20
print("x > 0 and x < 10:",x > 0 and x < 10)
print("x > 0 and y > 10:",x > 0 and y > 10)
print("x > 10 or y > 10:",x > 10 or y > 10)
print("x%2 == 0 and y%2 == 0:",x%2 == 0 and y%2 == 0)
print ("not (x+y>15):", not (x+y)>15)
它将生成以下 output −
It will produce the following output −
x > 0 and x < 10: False
x > 0 and y > 10: True
x > 10 or y > 10: True
x%2 == 0 and y%2 == 0: True
not (x+y>15): False
Example 2: Logical Operators With Non- Boolean Conditions
我们可以对逻辑运算符使用非布尔操作数。在此,我们需要注意到任何非零数字和非空序列都会被评估为 True。因此,会应用逻辑运算符的相同真值表。
We can use non-boolean operands with logical operators. Here, we need to not that any non-zero numbers, and non-empty sequences evaluate to True. Hence, the same truth tables of logical operators apply.
在以下示例中,数字操作数用于逻辑运算符。 variables "x"、"y" 评估为 True,"z" 为 False
In the following example, numeric operands are used for logical operators. The variables "x", "y" evaluate to True, "z" is False
x = 10
y = 20
z = 0
print("x and y:",x and y)
print("x or y:",x or y)
print("z or x:",z or x)
print("y or z:", y or z)
它将生成以下 output −
It will produce the following output −
x and y: 20
x or y: 10
z or x: 10
y or z: 20
Example 3: Logical Operators With Strings and Tuples
在以下示例中,字符串变量被视为 True,空元组被视为 False -
The string variable is treated as True and an empty tuple as False in the following example −
a="Hello"
b=tuple()
print("a and b:",a and b)
print("b or a:",b or a)
它将生成以下 output −
It will produce the following output −
a and b: ()
b or a: Hello
Example 4: Logical Operators To Compare Sequences (Lists)
最后,下面的两个列表对象非空。因此 x and y 返回后者,而 x or y 返回前者。
Finally, two list objects below are non-empty. Hence x and y returns the latter, and x or y returns the former.
x=[1,2,3]
y=[10,20,30]
print("x and y:",x and y)
print("x or y:",x or y)
它将生成以下 output −
It will produce the following output −
x and y: [10, 20, 30]
x or y: [1, 2, 3]