If I take out the following code, what should you do?
if not reset_excuted and (terminated or truncated): ... else: ...
Operator priority
In Python, the order in which Boolean operators are preceded by high to low is as follows:
- Brackets
()
: The highest priority, which can be used to clarify the order of operations. -
not
: The next highest priority. -
and
: The second lowest priority. -
or
: The lowest priority.
Priority parsing example
Example 1: Using brackets
a = True b = False c = True result = (a and not b) or c
In this example:
-
bracketsFirst calculated:
-
a and not b
middlenot b
Calculated asnot False
, the result isTrue
。 - Then,
True and True
Calculated asTrue
。
-
- Finally, the overall expression becomes
True or c
,turn outTrue
。
Example 2: No brackets are used
x = False y = True z = False result = x or y and not z
In this example:
-
PriorityFollow
not
>and
>or
:-
not z
Calculated asnot False
,turn outTrue
。
-
- Then the expression is converted to
x or y and True
。 - Next
y and True
Calculated asTrue
。 - The final calculation is
x or True
,turn outTrue
。
Complex examples
p = True q = False r = False result = not (p and q) or r
In this example:
-
bracketsFirst calculated:
-
p and q
Calculated asTrue and False
,turn outFalse
。
-
- Then,
not False
Calculated asTrue
。 - The final expression becomes
True or r
,turn outTrue
。
Complex if statement judgment
In expressionif not a and b
middle,not
Only righta
Take effect and will not affectb
。
-
not
The priority is higher thanand
, which means it will be processed firsta
value. - First calculate
not a
, this will returna
The opposite value of the boolean value. - Then, use
and
The operator combines the result withb
Make a comparison.
if not para_A and (para_B or para_C): print("Enter the branch above") else: print("Enter the branch below")
Go back to the beginning example and go through it carefully, in this code:
Here,not
Only rightpara_A
Take effect, not(para_B or para_C)
Take effect
To figure out under what circumstances to enter the upper branch or the lower branch, you can analyze each part of the condition.
Analysis conditions
-
not para_A
:Requirepara_A
forFalse
。- This means entering the above branch,
para_A
Must beFalse
。
- This means entering the above branch,
-
(para_B or para_C)
:Requirepara_B
orpara_C
At least one isTrue
。- This means as long as
para_B
forTrue
orpara_C
forTrue
, this part is established.
- This means as long as
Conditions to enter the above branch
The overall conditions arenot para_A and (para_B or para_C)
, so to enter the above branch, the following conditions must be met:
-
Condition 1:
para_A
yesFalse
。 -
Condition 2:
para_B
yesTrue
orpara_C
yesTrue
(At least one isTrue
)。
Conditions to enter the following branch
In order to enter the following branch, the condition needs to be invalid, i.e.:
-
Condition A:
para_A
yesTrue
。- At this time
not para_A
forFalse
, the conditions are not valid.
- At this time
-
Condition B:
para_A
yesFalse
,butpara_B
andpara_C
AllFalse
。- At this time
(para_B or para_C)
forFalse
, the conditions are not true.
- At this time
Summary condition table
para_A | para_B | para_C | result |
---|---|---|---|
False | True | False | Enter the above branch |
False | False | True | Enter the above branch |
False | True | True | Enter the above branch |
True | False | False | Enter the branch below |
True | True | True | Enter the branch below |
False | False | False | Enter the branch below |
in conclusion
-
Enter the above branch:when
para_A
forFalse
,andpara_B
orpara_C
At least one isTrue
。 -
Enter the branch below:when
para_A
forTrue
orpara_A
forFalse
,butpara_B
andpara_C
All forFalse
。
Multi-branch statement elif
I've written so much, so I'd just add something to make it look more complete
In Python,elif
is the abbreviation of "else if" and is used inif
Multiple conditional judgments are made in the statement. It allows you to be on the firstif
The conditions areFalse
Continue to check other conditions in the case, thereby achieving more branch logic.
if condition1: # Code executed when condition1 is Trueelif condition2: # Code executed when condition1 is False and condition2 is Trueelif condition3: # Code executed when both condition1 and condition2 are False and condition3 are Trueelse: # When all the above conditions are False code executed when
DeMorgan's Law
In actual j code application, you basically can't use this law, and the above things can already solve most problems. But if the program must beif
Attack you in the conditional statement, at least you know how to deal with it.
DeMorgan's law is two important laws in Boolean algebra that provide important formulas about the relationship between logical operations (and, or and non). These two laws are as follows:
Article 1 Law:
not(A or B)≡notA and notB
Explanation: NegativeA or B
Equivalent toA
andB
Negative relationship.
The second law:
not(A and B)≡notA or notB
Explanation: NegativeA and B
Equivalent toA
Negation orB
Need.
Give an example
We can understand these laws through several examples:
Example 1: The First Law
considerA = True
andB = False
:
- calculate
not(A or B)
:-
A or B
yesTrue
-
not(A or B)
yesFalse
-
- calculate
not A and not B
:-
not A
yesFalse
-
not B
yesTrue
-
not A and not B
yesFalse
-
The results are consistent:not(A or B) = False
andnot A and not B = False
。
Example 2: The Second Law
considerA = True
andB = False
:
- calculate
not(A and B)
:-
A and B
yesFalse
-
not(A and B)
yesTrue
-
- calculate
not A or not B
:-
not A
yesFalse
-
not B
yesTrue
-
not A or not B
yesTrue
-
Again, the results are equal:not(A and B) = True
andnot A or not B = True
。
This is the end of this article about the full explanation of Python conditional branch if statement. For more related Python conditional branch if statement content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!