logo

Betingede udtryk i Python

Pythons betingede udsagn udfører forskellige beregninger eller operationer alt efter om en bestemt boolsk tilstand vurderes som sand eller falsk. I Python omhandler IF-udsagn betingede udsagn.

Vi lærer, hvordan du bruger betingede udsagn i Python i denne øvelse.

java til json objekt

Hvad er Python If Statement?

For at træffe beslutninger skal du bruge if-sætningen i Python. Den har en række instruktioner, der kun udføres, når if-sætningens betingelse er opfyldt. Den yderligere else-sætning, som indeholder nogle instruktioner til else-sætningen, kører, hvis betingelsen if er falsk.

Pythons if-else-sætning bruges, når du ønsker at opfylde et udsagn, mens det andet er falsk.

Python-syntaks for if-sætningen:

 if Statement else Statement 

Kode

 # Python program to execute if statement a, b = 6, 5 # Initializing the if condition if a > b: code = 'a is greater than b' print(code) 

Produktion:

 a is greater than b 

Hvordan bruger man den anden betingelse?

'andet betingelse' bruges normalt, når man bedømmer et udsagn ud fra et andet. Hvis betingelsen nævnt i if-kodeblokken er forkert, vil tolken udføre den anden kodeblok.

Kode

 # Python program to execute if-else statement a, b = 6, 5 # Initializing the if-else condition if a <b: code="a is less than b" print(code) else: print('a is greater than b') < pre> <p> <strong>Output:</strong> </p> <pre> a is greater than b </pre> <h2>When the else Statement does not Work</h2> <p>There could be a lot of situations where your &apos;otherwise condition&apos; doesn&apos;t produce the desired outcome. Due to a flaw in the program&apos;s logic, it will print the incorrect result. This typically occurs when there are more than two statements or conditions in a program.</p> <p>An illustration will make this notion easier for you to grasp.</p> <p>Since both variables, in this case, are identical (9, 9), the program&apos;s output that &apos;x is greater than y&apos; is FALSE. This is because it evaluates the first condition, or the if expression in Python, then prints the next condition (the else statement) by default if the first condition fails. The following step will examine how to fix this mistake.</p> <p> <strong>Code</strong> </p> <pre> # Python program when else condition does not work a, b = 9, 9 # Initializing the if-else condition if a <b: code="a is less than b" else: print(code) < pre> <p> <strong>Output:</strong> </p> <pre> a is greater than b </pre> <h2>How to use the elif Condition?</h2> <p>We can employ the &apos;elif&apos; clause to fix the issue caused by the &apos;else condition&apos; made earlier. You can instruct the software to print the third condition or alternative when the first two conditions fail or are erroneous by using the &apos;elif&apos; condition.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to use elif condition a, b = 9, 9 # Initializing the if-else condition if a <b: code="a is less than b" elif a="=" b: else: print(code) < pre> <p> <strong>Output:</strong> </p> <pre> a is equal to b </pre> <h2>Python Nested if Statement</h2> <p>The following example demonstrates nested if Statement Python</p> <p> <strong>Code</strong> </p> <pre> # Python program to show the nested if-else conditions A = 100 B = 200 C = 300 # Initializing the if-else conditions if B &gt; A: if B &gt; C: print(&apos;B is the largest number&apos;) else: if A &gt; B: if A &gt; C: print(&apos;A is the largest number&apos;) elif C &gt; A: if C &gt; B: print(&apos;C is the largest number&apos;) else: print(&apos;All numbers are equal&apos;) if B % C == 0: if A % C == 0: print(&apos;C is a common factor of A and B&apos;) </pre> <p> <strong>Output:</strong> </p> <pre> C is the largest number </pre> <hr></b:></pre></b:></pre></b:>

Når den anden erklæring ikke virker

Der kan være mange situationer, hvor din 'ellers tilstand' ikke giver det ønskede resultat. På grund af en fejl i programmets logik vil det udskrive det forkerte resultat. Dette sker typisk, når der er mere end to udsagn eller betingelser i et program.

En illustration vil gøre dette begreb lettere for dig at forstå.

Da begge variabler i dette tilfælde er identiske (9, 9), er programmets output, at 'x er større end y', FALSK. Dette skyldes, at den evaluerer den første betingelse, eller udtrykket if i Python, og derefter udskriver den næste betingelse (else-sætningen) som standard, hvis den første betingelse mislykkes. Det følgende trin vil undersøge, hvordan du løser denne fejl.

Kode

 # Python program when else condition does not work a, b = 9, 9 # Initializing the if-else condition if a <b: code="a is less than b" else: print(code) < pre> <p> <strong>Output:</strong> </p> <pre> a is greater than b </pre> <h2>How to use the elif Condition?</h2> <p>We can employ the &apos;elif&apos; clause to fix the issue caused by the &apos;else condition&apos; made earlier. You can instruct the software to print the third condition or alternative when the first two conditions fail or are erroneous by using the &apos;elif&apos; condition.</p> <p> <strong>Code</strong> </p> <pre> # Python program to show how to use elif condition a, b = 9, 9 # Initializing the if-else condition if a <b: code="a is less than b" elif a="=" b: else: print(code) < pre> <p> <strong>Output:</strong> </p> <pre> a is equal to b </pre> <h2>Python Nested if Statement</h2> <p>The following example demonstrates nested if Statement Python</p> <p> <strong>Code</strong> </p> <pre> # Python program to show the nested if-else conditions A = 100 B = 200 C = 300 # Initializing the if-else conditions if B &gt; A: if B &gt; C: print(&apos;B is the largest number&apos;) else: if A &gt; B: if A &gt; C: print(&apos;A is the largest number&apos;) elif C &gt; A: if C &gt; B: print(&apos;C is the largest number&apos;) else: print(&apos;All numbers are equal&apos;) if B % C == 0: if A % C == 0: print(&apos;C is a common factor of A and B&apos;) </pre> <p> <strong>Output:</strong> </p> <pre> C is the largest number </pre> <hr></b:></pre></b:>

Hvordan bruger man elif Condition?

Vi kan bruge 'elif'-klausulen til at løse problemet forårsaget af 'else-betingelsen', der blev lavet tidligere. Du kan instruere softwaren til at udskrive den tredje betingelse eller alternativ, når de to første betingelser fejler eller er fejlagtige ved at bruge 'elif'-betingelsen.

Kode

 # Python program to show how to use elif condition a, b = 9, 9 # Initializing the if-else condition if a <b: code="a is less than b" elif a="=" b: else: print(code) < pre> <p> <strong>Output:</strong> </p> <pre> a is equal to b </pre> <h2>Python Nested if Statement</h2> <p>The following example demonstrates nested if Statement Python</p> <p> <strong>Code</strong> </p> <pre> # Python program to show the nested if-else conditions A = 100 B = 200 C = 300 # Initializing the if-else conditions if B &gt; A: if B &gt; C: print(&apos;B is the largest number&apos;) else: if A &gt; B: if A &gt; C: print(&apos;A is the largest number&apos;) elif C &gt; A: if C &gt; B: print(&apos;C is the largest number&apos;) else: print(&apos;All numbers are equal&apos;) if B % C == 0: if A % C == 0: print(&apos;C is a common factor of A and B&apos;) </pre> <p> <strong>Output:</strong> </p> <pre> C is the largest number </pre> <hr></b:>

Python Nested if Statement

Følgende eksempel viser indlejret if Statement Python

Kode

 # Python program to show the nested if-else conditions A = 100 B = 200 C = 300 # Initializing the if-else conditions if B &gt; A: if B &gt; C: print(&apos;B is the largest number&apos;) else: if A &gt; B: if A &gt; C: print(&apos;A is the largest number&apos;) elif C &gt; A: if C &gt; B: print(&apos;C is the largest number&apos;) else: print(&apos;All numbers are equal&apos;) if B % C == 0: if A % C == 0: print(&apos;C is a common factor of A and B&apos;) 

Produktion:

 C is the largest number