Python: Conditional Branches if Statements Explained
If I produce the following code, what should Your Excellency do?
if not reset_excuted and (terminated or truncated):
...
else:
...
----
Preface:
My brain froze when I saw this while digesting the code for my thesis, I didn't think I'd struggle with something so basic!
This article was first edited on 2024.10.10
CSDN Home Page:/rvdgdsva
Blogland Home Page:/hassle
-
Python: Conditional Branches if Statements Explained
- Preface:
-
operator priority
-
Example of Prioritization Resolution
- Example 1: Using Parentheses
- Example 2: Without Parentheses
- Complex Example
-
Example of Prioritization Resolution
-
Complex if statement judgment
-
analysis condition
- Conditions for entering the above branch
- Conditions for entering the following branch
- Summary Condition Table
- reach a verdict
-
analysis condition
- Multi-branch statement elif
-
De Morgan's law
-
give an example
- Example 1: The First Law
- Example 2: The Second Law
-
give an example
operator priority
In Python, Boolean operators have the following order of precedence, from highest to lowest:
-
braces
()
: The highest priority, which can be used to clarify the order of operations. -
not
: Second highest priority. -
and
: Second lowest priority. -
or
: Lowest priority.
Example of Prioritization Resolution
Example 1: Using Parentheses
a = True
b = False
c = True
result = (a and not b) or c
In this example:
-
braces First to be counted:
-
a and not b
centernot b
count asnot False
The results areTrue
。 - Then.
True and True
count asTrue
。
-
- Finally, the overall expression becomes
True or c
The result isTrue
。
Example 2: Without Parentheses
x = False
y = True
z = False
result = x or y and not z
In this example:
-
prioritization on the basis of
not
>and
>or
:-
not z
count asnot False
The result isTrue
。
-
- The expression is then converted to
x or y and True
。 - go on to do sth
y and True
count asTrue
。 - The final calculation is
x or True
The result isTrue
。
Complex Example
p = True
q = False
r = False
result = not (p and q) or r
In this example:
-
braces First to be counted:
-
p and q
count asTrue and False
The result isFalse
。
-
- Then.
not False
count asTrue
。 - The final expression becomes
True or r
The result isTrue
。
Complex if statement judgment
in the expressionif not a and b
Middle.not
just fora
Entry into force without prejudice tob
。
-
not
has a higher priority than theand
This means that it will be processed firsta
The value of the - first calculation
not a
This will returna
is the opposite of the boolean value of - Then, use the
and
operator compares the result with theb
Make comparisons.
if not para_A and (para_B or para_C)::
print("Going to the above branch.")
print("Go to the branch above"): print("Go to the branch below")
print("Go to the following branch").
Go back to the opening example and run through the code in detail:
Here.not
just forpara_A
effective, and not on the(para_B or para_C)
go into effect
To project under what circumstances you enter the branch above or the branch below, you can analyze each part of the condition.
analysis condition
-
not para_A
: Requirementspara_A
because ofFalse
。- This means that to get to the branch above
para_A
must beFalse
。
- This means that to get to the branch above
-
(para_B or para_C)
: Requirementspara_B
maybepara_C
At least one of them isTrue
。- This means that as long as
para_B
because ofTrue
maybepara_C
because ofTrue
, this part stands.
- This means that as long as
Conditions for entering 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
beFalse
。 -
Condition 2:
para_B
beTrue
maybepara_C
beTrue
(At least one of them isTrue
)。
Conditions for entering the following branch
In order to get to the following branch, the condition needs to not hold, ie:
-
Condition A:
para_A
beTrue
。- at this time
not para_A
because ofFalse
, the condition would not hold.
- at this time
-
Condition B:
para_A
beFalse
Libyan Arab Jamahiriyapara_B
respond in singingpara_C
allFalse
。- at this time
(para_B or para_C)
because ofFalse
, and the conditions do not hold.
- at this time
Summary Condition Table
para_A |
para_B |
para_C |
in the end |
---|---|---|---|
False | True | False | Go to the branch above |
False | False | True | Go to the branch above |
False | True | True | Go to the branch above |
True | False | False | Go to the following branch |
True | True | True | Go to the following branch |
False | False | False | Go to the following branch |
reach a verdict
-
Go to the branch aboveDang.
para_A
because ofFalse
andpara_B
maybepara_C
At least one of them isTrue
。 -
Go to the following branch(coll.) ding dong
para_A
because ofTrue
orpara_A
because ofFalse
Libyan Arab Jamahiriyapara_B
cap (a poem)para_C
all areFalse
。
Multi-branch statement elif
I've written so much, why don't I just add a few more things to make it more complete?
In Python, theelif
is an abbreviation for "else if", which is used in theif
statements to make multiple conditional judgments. It allows you to make multiple conditional judgments in the firstif
conditioned onFalse
case to continue checking other conditions, thus enabling more branching logic.
if condition1.
# Code to execute when condition1 is True
elif condition2.
# Code to execute when condition1 is False and condition2 is True
elif condition3.
# Code to execute when condition1 and condition2 are both False and condition3 is True
else.
# Code to be executed when all the above conditions are False
De Morgan's law
In j actual code application, you basically can't use this law, the above stuff can already solve the vast majority of problems. But if the program has to be inif
Conditional statements come at you, and at least you know how to deal with them.
De Morgan's laws are two important laws in Boolean algebra that provide important formulas about the relationships between logical operations (with, or, and not). These two laws are as follows:
the First Law:
Explanation: NegativeA or B
equivalent toA
respond in singingB
The negation of the phase with.
The second law.:
Explanation: NegativeA and B
equivalent toA
the negative orB
The denial.
give an example
We can understand these laws with a few examples:
Example 1: The First Law
think overA = True
cap (a poem)B = False
:
- count
not(A or B)
:-
A or B
beTrue
-
not(A or B)
beFalse
-
- count
not A and not B
:-
not A
beFalse
-
not B
beTrue
-
not A and not B
beFalse
-
The results are consistent:not(A or B) = False
respond in singingnot A and not B = False
。
Example 2: The Second Law
think overA = True
cap (a poem)B = False
:
- count
not(A and B)
:-
A and B
beFalse
-
not(A and B)
beTrue
-
- count
not A or not B
:-
not A
beFalse
-
not B
beTrue
-
not A or not B
beTrue
-
Again, the results are equivalent:not(A and B) = True
cap (a poem)not A or not B = True
。