In high school mathematics, we have learned logical operations. For example, p is a true proposition, q is a false proposition, then "p and q" is false, "p or q" is true, and "non-q" is true. Python has similar logical operations, please see the following table:
Logical Operators | Operator Descriptions | Basic Format | Explanations |
---|---|---|---|
>and | Logical AND operation, Equivalent to "and" in mathematics | a and b | The result of a and b is true, when both a and b expressions are true, otherwise it is false. |
>or | Logical OR operation, Equivalent to "or" in mathematics | a or b | The result of a or b is false, when both a and b expressions are false, otherwise it is true. |
>not | Logical NOT operation, equivalent to "not" in mathematics | not a | If a is true, then the result of not a is false; if a is false, then the result of not a is true. Equivalent to opposite of a. |
For example:
Some irresponsible Python tutorials say: Python logical operators are used to manipulate expressions of bool type, and the execution result is also bool type. Both are actually wrong!
Python logical operators can be used to manipulate any type of expression, regardless of whether the expression is a bool type; at the same time, the result of a logical operation is not necessarily a bool type, and it can be of any type. Consider the following example:
print(100 and 200) print(45 and 0) print("" or "Welcome to Free Learning Points") print(18.5 or "Welcome to Free Learning Points")
The output is:
200
0
Welcome to Free Learning Points
18.5
From the above example, neither the AND and OR operators operate on bool-type expressions, and the result of the operation is not a bool value.
In Python, AND and OR do not necessarily calculate the value of the expression on the right-hand side, and sometimes only calculate the value of the expression on the left-hand side therefore able to get the result.
In addition, the AND and OR operators take one of the expression values as the result, rather than True or False as the result.
The above two points are extremely important. After understanding these two points will help you in the process of using logical operations.
As for the AND operator, the result is true when the values on both sides are true, but if one of the values is false, then the result is false, so Python performs AND operation according to the following rules:
a. If the value of the left-hand side expression is false, then the value of the right-hand side expression does not need to be calculated, because no matter what the value of the right expression is, it will not affect the result. The result is still false. At this time, AND operation will express the left-hand side expression as the result.
b. If the value of the left-hand side expression is true, the final value cannot be determined, AND operation will continue to calculate the value of the right-hand side expression therefore use the value of the right-hand side expression as the result.
As for the OR operator, the situation is similar to AND operator. The result is false when the values on both sides are false. If one of the values is true, the result is true.
If the value of the left-hand side expression is true, then the value of the right-hand side expression does not need to be calculated, because no matter what the value of the right expression is, it will not affect the result and it turns out the result is true.
If the value of the left-hand side expression is false, the final value cannot be determined, or will continue to calculate the value of the right-hand side expression and use the value of the right-hand side expression as the result.
Example:
str = "Welcome to Free Learning Points" print("--------False and xxx---------") print(False and print(str)) print("--------True and xxx---------") print( True and print(str)) print("--------False or xxx---------") print( False or print(str)) print("--------True or xxx---------") print( True or print(str))
The output is:
--------False and xxx---------
False
--------True and xxx---------
Welcome to Free Learning Points
None
--------False or xxx---------
Welcome to Free Learning Points
None
--------True or xxx---------
True
In the line 34 of code, the value on the left-hand side of AND is false. There is no need to execute the expression on the right-hand side, so print (str) has no output.
In line 35, the value on the left-hand side of AND is true, and the expression on the right-hand side needs to be executed in order to get the result, so print (str) outputs a string.
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises