Python's Logical Values and Relational/Logical Operations
Logical Values (Booleans)
There are only two logical values: one represents truth, and the other represents falsehood.
One is the English word True, and the other is the English word False, note the case sensitivity.
In computers, for convenience, True corresponds to the numerical value 1, and False corresponds to the numerical value 0.
You can verify this by printing int(True)
and int(False)
.
Logical Operations
When you need to combine logical results, simple relational operations are often insufficient. Logical operations are then required to combine the results of relational operations.
Logical operations are often similar to conjunctions in Chinese grammar, such as “and” and “or.”
There are three logical operators:
- Logical NOT operator:
not
- Logical AND operator (and):
and
- Logical OR operator (or):
or
NOT Operation Results
a’s Value | NOT Operation Result (not a) |
---|---|
True | False |
False | True |
AND (and) Operation Results
a | b | a and b Result |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
That is, if either side is False, the result is False.
OR (or) Operation Results
a | b | a or b Result |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
That is, if either side is True, the result is True.
Combining Relational and Logical Operations Examples
For example:
a = 10 > 5 and 5 > 1, which is equivalent to a = True and True, and the final result is: True
This is also the most standard way to write it. The usage of “or” is similar.
In fact, the above example can also be written in Python as a = 10 > 5 > 1, and the final result is True
Because the above example in Python is specially interpreted as a = 10 > 5 and 5 > 1, which is defined by the syntax. Note that languages like C cannot execute this way.
However, it is also important to note that if your expression is: a = (10 > 5) > 1, this is equivalent to a = True > 1, which is a = 1 > 1, and the final result is False.
That is, if you specify step-by-step operations, you must perform operations based on the results of each step.
Thinking Exercises
Highest Score Problem
Problem Analysis
Mid-term exam, there are only 3 subjects (just an assumption), input the scores of these three subjects, and output how many of them reached excellence (we still define excellence as the score range of 100-90, of course, our input scores are logical, that is, the highest score is 100, and the lowest is 0)
The meaning of this problem is to determine whether the three input scores are higher than 90 points, that is, to meet the standard of excellence.
Input and Splitting the List
We need to input the scores of three subjects, and these three numbers must be on one line, so we need to use the split list
1 | a = input("Enter the scores of three subjects:") |
Score Comparison
Here we need to use logical operations. Here we define that if the score is greater than 90, it meets the condition and is excellent, with a feedback value of 1; if it is less than 90, it does not meet the condition and the feedback value is 0
1 | f = c >= 90 |
Output of Results
The result of each subject can only be 0 or 1, adding these numbers together can find out how many subjects are excellent, and finally output them, adding these numbers together
1 | i = int(f) + int(g) + int(h) |
Code Example
1 | # Whether the score is excellent, logical operation |