logo

التعبيرات الشرطية في بايثون

تنفذ عبارات بايثون الشرطية حسابات أو عمليات مختلفة وفقًا لما إذا تم تقييم شرط منطقي معين على أنه صحيح أم خطأ. في بايثون، تتعامل عبارات IF مع العبارات الشرطية.

سوف نتعلم كيفية استخدام العبارات الشرطية في لغة بايثون في هذا البرنامج التعليمي.

ما هو بيثون إذا البيان؟

لاتخاذ القرارات، استخدم عبارة if في بايثون. يحتوي على مجموعة من التعليمات التي يتم تنفيذها فقط عند استيفاء شرط عبارة if. يتم تشغيل عبارة else الإضافية، والتي تتضمن بعض التعليمات الخاصة بعبارة else، إذا كان شرط if خاطئًا.

يتم استخدام عبارة if-else الخاصة ببايثون عندما ترغب في تلبية عبارة واحدة بينما تكون الأخرى خاطئة.

بناء جملة بايثون لبيان if:

 if Statement else Statement 

شفرة

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

انتاج:

 a is greater than b 

كيفية استخدام شرط آخر؟

يتم استخدام 'الشرط الآخر' عادةً عند الحكم على عبارة بناءً على أخرى. إذا كان الشرط المذكور في مقطع التعليمات البرمجية if خاطئًا، فسيقوم المترجم بتنفيذ مقطع التعليمات البرمجية else.

شفرة

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

عندما لا يعمل بيان آخر

قد يكون هناك الكثير من المواقف التي لا تؤدي فيها 'حالتك بخلاف ذلك' إلى النتيجة المرجوة. بسبب وجود خلل في منطق البرنامج، فإنه سيتم طباعة النتيجة غير الصحيحة. يحدث هذا عادةً عندما يكون هناك أكثر من عبارة أو شرطين في البرنامج.

التوضيح سيجعل هذه الفكرة أسهل بالنسبة لك لفهمها.

وبما أن كلا المتغيرين، في هذه الحالة، متطابقان (9، 9)، فإن مخرجات البرنامج التي تكون 'x أكبر من y' تكون خاطئة. وذلك لأنه يقوم بتقييم الشرط الأول، أو تعبير if في بايثون، ثم يطبع الشرط التالي (عبارة else) افتراضيًا في حالة فشل الشرط الأول. سوف تدرس الخطوة التالية كيفية إصلاح هذا الخطأ.

شفرة

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

كيفية استخدام حالة إليف؟

يمكننا استخدام جملة 'elif' لإصلاح المشكلة الناجمة عن 'الشرط الآخر' الذي تم إجراؤه مسبقًا. يمكنك توجيه البرنامج لطباعة الشرط الثالث أو البديل عند فشل الشرطين الأولين أو عندما يكونان خاطئين باستخدام شرط 'elif'.

شفرة

 # 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 متداخلاً

شفرة

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

انتاج:

 C is the largest number