Location>code7788 >text

Python: Conditional Branches if Statements Explained

Popularity:501 ℃/2024-10-10 17:28:21

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!

Even an ice warrior can learn from this.I guess so.

This article was first edited on 2024.10.10

CSDN Home Page:/rvdgdsva

Blogland Home Page:/hassle


catalogs
  • Python: Conditional Branches if Statements Explained
    • Preface:
    • operator priority
      • Example of Prioritization Resolution
        • Example 1: Using Parentheses
        • Example 2: Without Parentheses
        • Complex Example
    • Complex if statement judgment
      • analysis condition
        • Conditions for entering the above branch
        • Conditions for entering the following branch
      • Summary Condition Table
      • reach a verdict
    • Multi-branch statement elif
    • De Morgan's law
      • give an example
        • Example 1: The First Law
        • Example 2: The Second Law


operator priority

In Python, Boolean operators have the following order of precedence, from highest to lowest:

  1. braces(): The highest priority, which can be used to clarify the order of operations.
  2. not: Second highest priority.
  3. and: Second lowest priority.
  4. 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:

  1. braces First to be counted:
    • a and not b centernot b count asnot FalseThe results areTrue
    • Then.True and True count asTrue
  2. Finally, the overall expression becomesTrue or cThe result isTrue

Example 2: Without Parentheses

x = False
y = True
z = False

result = x or y and not z

In this example:

  1. prioritization on the basis ofnot > and > or
    • not z count asnot FalseThe result isTrue
  2. The expression is then converted tox or y and True
  3. go on to do sthy and True count asTrue
  4. The final calculation isx or TrueThe result isTrue

Complex Example

p = True
q = False
r = False

result = not (p and q) or r

In this example:

  1. braces First to be counted:
    • p and q count asTrue and FalseThe result isFalse
  2. Then.not False count asTrue
  3. The final expression becomesTrue or rThe 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 theandThis means that it will be processed firsta The value of the
  • first calculationnot aThis will returna is the opposite of the boolean value of
  • Then, use theand 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.notjust forpara_Aeffective, 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

  1. not para_A: Requirementspara_A because ofFalse

    • This means that to get to the branch abovepara_A must beFalse
  2. (para_B or para_C): Requirementspara_B maybepara_C At least one of them isTrue

    • This means that as long aspara_B because ofTrue maybepara_C because ofTrue, this part stands.

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 1para_A beFalse
  • Condition 2para_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:

  1. Condition Apara_A beTrue

    • at this timenot para_A because ofFalse, the condition would not hold.
  2. Condition Bpara_A beFalseLibyan Arab Jamahiriyapara_B respond in singingpara_C allFalse

    • at this time(para_B or para_C) because ofFalse, and the conditions do not hold.

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 ofFalseandpara_B maybepara_C At least one of them isTrue
  • Go to the following branch(coll.) ding dongpara_A because ofTrue orpara_A because ofFalseLibyan 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 inifConditional 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

\[\text{not}(A\ or\ B) \equiv \text{not} A \ and\ \text{not} B \]

Explanation: NegativeA or B equivalent toA respond in singingB The negation of the phase with.

The second law.

\[\text{not}(A \ and\ B) \equiv \text{not} A \ or\ \text{not} B \]

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

  • countnot(A or B)
    • A or B beTrue
    • not(A or B) beFalse
  • countnot 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

  • countnot(A and B)
    • A and B beFalse
    • not(A and B) beTrue
  • countnot 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