logo

Bedingte Ausdrücke in Python

Die bedingten Anweisungen von Python führen verschiedene Berechnungen oder Operationen aus, je nachdem, ob eine bestimmte boolesche Bedingung als wahr oder falsch ausgewertet wird. In Python befassen sich IF-Anweisungen mit bedingten Anweisungen.

Java-Codierung, if else-Anweisung

In diesem Tutorial erfahren Sie, wie Sie bedingte Anweisungen in Python verwenden.

Was ist eine Python-If-Anweisung?

Um Entscheidungen zu treffen, verwenden Sie die if-Anweisung in Python. Es verfügt über einen Befehlskörper, der nur dann ausgeführt wird, wenn die Bedingung der if-Anweisung erfüllt ist. Die zusätzliche else-Anweisung, die einige Anweisungen für die else-Anweisung enthält, wird ausgeführt, wenn die if-Bedingung falsch ist.

Die if-else-Anweisung von Python wird verwendet, wenn Sie eine Anweisung erfüllen möchten, während die andere falsch ist.

Python-Syntax der if-Anweisung:

 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) 

Ausgabe:

 a is greater than b 

Wie verwende ich die else-Bedingung?

Die „else-Bedingung“ wird normalerweise verwendet, wenn eine Aussage auf der Grundlage einer anderen beurteilt wird. Wenn die im if-Codeblock genannte Bedingung falsch ist, führt der Interpreter den else-Codeblock aus.

foreach-Schleifen-Typoskript

Code

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

Wenn die else-Anweisung nicht funktioniert

Es kann viele Situationen geben, in denen Ihre „Sonst-Bedingung“ nicht zum gewünschten Ergebnis führt. Aufgrund eines Fehlers in der Programmlogik wird ein falsches Ergebnis ausgegeben. Dies tritt normalerweise auf, wenn ein Programm mehr als zwei Anweisungen oder Bedingungen enthält.

Eine Illustration soll Ihnen das Verständnis dieses Gedankens erleichtern.

Da beide Variablen in diesem Fall identisch sind (9, 9), ist die Ausgabe des Programms, dass „x größer als y“ ist, FALSCH. Dies liegt daran, dass es die erste Bedingung oder den if-Ausdruck in Python auswertet und dann standardmäßig die nächste Bedingung (die else-Anweisung) ausgibt, wenn die erste Bedingung fehlschlägt. Im folgenden Schritt wird untersucht, wie dieser Fehler behoben werden kann.

Code

Welche Größe hat mein Monitorbildschirm?
 # 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:>

Wie verwende ich die elif-Bedingung?

Wir können die „elif“-Klausel verwenden, um das Problem zu beheben, das durch die zuvor erstellte „else-Bedingung“ verursacht wurde. Sie können die Software anweisen, die dritte Bedingung oder Alternative zu drucken, wenn die ersten beiden Bedingungen fehlschlagen oder fehlerhaft sind, indem Sie die Bedingung „elif“ verwenden.

Code

String-Java-Array
 # 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 verschachtelte if-Anweisung

Das folgende Beispiel demonstriert eine verschachtelte if-Anweisung in Python

Code

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

Ausgabe:

 C is the largest number