SoFunction
Updated on 2025-03-02

Python conditional branch if statements are explained (mastered in one article)

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 bmiddlenot bCalculated asnot False, the result isTrue
    • Then,True and TrueCalculated asTrue
  • Finally, the overall expression becomesTrue 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:

  • PriorityFollownot > and > or
    • not zCalculated asnot False,turn outTrue
  • Then the expression is converted tox or y and True
  • Nexty and TrueCalculated asTrue
  • The final calculation isx 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 qCalculated asTrue and False,turn outFalse
  • Then,not FalseCalculated asTrue
  • The final expression becomesTrue or r,turn outTrue

Complex if statement judgment

In expressionif not a and bmiddle,notOnly rightaTake effect and will not affectb

  • notThe priority is higher thanand, which means it will be processed firstavalue.
  • First calculatenot a, this will returnaThe opposite value of the boolean value.
  • Then, useandThe operator combines the result withbMake 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,notOnly rightpara_ATake 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_AforFalse

    • This means entering the above branch,para_AMust beFalse
  • (para_B or para_C):Requirepara_Borpara_CAt least one isTrue

    • This means as long aspara_BforTrueorpara_CforTrue, this part is established.

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 1para_AyesFalse
  • Condition 2para_ByesTrueorpara_CyesTrue(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 Apara_AyesTrue

    • At this timenot para_AforFalse, the conditions are not valid.
  • Condition Bpara_AyesFalse,butpara_Bandpara_CAllFalse

    • At this time(para_B or para_C)forFalse, the conditions are not true.

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:whenpara_AforFalse,andpara_Borpara_CAt least one isTrue
  • Enter the branch below:whenpara_AforTrueorpara_AforFalse,butpara_Bandpara_CAll forFalse

Multi-branch statement elif

I've written so much, so I'd just add something to make it look more complete

In Python,elifis the abbreviation of "else if" and is used inifMultiple conditional judgments are made in the statement. It allows you to be on the firstifThe conditions areFalseContinue 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 beifAttack 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 BEquivalent toAandBNegative relationship.

The second law

not(A and B)≡notA or notB

Explanation: NegativeA and BEquivalent toANegation orBNeed.

Give an example

We can understand these laws through several examples:

Example 1: The First Law

considerA = TrueandB = False

  • calculatenot(A or B)
    • A or ByesTrue
    • not(A or B)yesFalse
  • calculatenot A and not B
    • not AyesFalse
    • not ByesTrue
    • not A and not ByesFalse

The results are consistent:not(A or B) = Falseandnot A and not B = False

Example 2: The Second Law

considerA = TrueandB = False

  • calculatenot(A and B)
    • A and ByesFalse
    • not(A and B)yesTrue
  • calculatenot A or not B
    • not AyesFalse
    • not ByesTrue
    • not A or not ByesTrue

Again, the results are equal:not(A and B) = Trueandnot 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!