logo

Voorwaardelijke expressies in Python

De voorwaardelijke instructies van Python voeren verschillende berekeningen of bewerkingen uit, afhankelijk van of een bepaalde Booleaanse voorwaarde als waar of onwaar wordt geëvalueerd. In Python behandelen IF-instructies voorwaardelijke instructies.

In deze zelfstudie leren we hoe u voorwaardelijke instructies in Python kunt gebruiken.

Wat is Python If-instructie?

Gebruik de if-instructie in Python om beslissingen te nemen. Het heeft een reeks instructies die alleen worden uitgevoerd als aan de voorwaarde van de if-instructie is voldaan. De aanvullende else-instructie, die enkele instructies voor de else-instructie bevat, wordt uitgevoerd als de if-voorwaarde onwaar is.

De if-else-instructie van Python wordt gebruikt als je aan de ene instructie wilt voldoen terwijl de andere onwaar is.

Python-syntaxis van de if-instructie:

 if Statement else Statement 

Code

 # 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) 

Uitgang:

 a is greater than b 

Hoe gebruik ik de else-voorwaarde?

De 'else-voorwaarde' wordt meestal gebruikt bij het beoordelen van de ene uitspraak op basis van de andere. Als de voorwaarde vermeld in het if-codeblok onjuist is, zal de tolk het else-codeblok uitvoeren.

Code

annotaties in springlaars
 # 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:>

Wanneer de else-verklaring niet werkt

Er kunnen veel situaties zijn waarin uw ‘anderszins toestand’ niet het gewenste resultaat oplevert. Vanwege een fout in de logica van het programma wordt het onjuiste resultaat afgedrukt. Dit gebeurt doorgaans als er meer dan twee instructies of voorwaarden in een programma voorkomen.

Een illustratie zal dit begrip voor u gemakkelijker te begrijpen maken.

Omdat beide variabelen in dit geval identiek zijn (9, 9), is de uitvoer van het programma dat 'x groter is dan y' FALSE. Dit komt omdat het de eerste voorwaarde evalueert, of de if-expressie in Python, en vervolgens standaard de volgende voorwaarde (de else-instructie) afdrukt als de eerste voorwaarde mislukt. In de volgende stap wordt onderzocht hoe u deze fout kunt herstellen.

Code

 # 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:>

Hoe gebruik je de elif-voorwaarde?

We kunnen de 'elif'-clausule gebruiken om het probleem op te lossen dat wordt veroorzaakt door de eerder gemaakte 'else condition'. U kunt de software instrueren om de derde voorwaarde of alternatief af te drukken wanneer de eerste twee voorwaarden falen of onjuist zijn, door de 'elif'-voorwaarde te gebruiken.

Code

 # 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 geneste if-instructie

In het volgende voorbeeld wordt de geneste if-instructie Python gedemonstreerd

Code

c++ gesplitste tekenreeks
 # 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;) 

Uitgang:

 C is the largest number