Python の条件文:IF…Else、ELIF、Switch Case
Python の条件文とは?
Python の条件付きステートメントは、特定のブール制約が true または false に評価されるかどうかに応じて、異なる計算またはアクションを実行します。条件ステートメントは、Python の IF ステートメントによって処理されます。
このチュートリアルでは、Python で条件ステートメントを適用する方法について説明します。
- If ステートメントとは?それの使い方?
- 「if条件」が満たされない場合
- 「else 条件」の使い方
- 「else条件」がうまくいかない場合
- 「elif」条件の使い方
- 最小限のコードで条件文を実行する方法
- ネストされた Python の if ステートメント
- Python の Switch Case ステートメント
Python If ステートメントとは
Python if ステートメント 意思決定操作に使用されます。 if ステートメントで指定された条件が true の場合にのみ実行されるコード本体が含まれています。条件が false の場合、else 条件のコードを含むオプションの else ステートメントが実行されます。
一方の条件が真でなく、他方の条件が真でないことを正当化したい場合は、Python の if else ステートメントを使用します。
Python if ステートメントの構文:
if expression Statement else Statement
Python if…else フローチャート
Python の if else ステートメントの例を見てみましょう:
# #Example file for working with conditional statement # def main(): x,y =2,8 if(x < y): st= "x is less than y" print(st) if __name__ == "__main__": main()
- コード 5 行目:2 つの変数 x、y =2、8 を定義します
- コード 7 行目:Python の if ステートメントは、条件 x
True であることを確認します。 この場合 - コード 8 行目:変数 st が「x は y より小さい」に設定されています。
- コード 9 行目:行 print st は変数 st の値を出力します。これは「x は y より小さい」です。
「if 条件」が満たされない場合
このステップでは、Python の if 条件が満たされない場合に何が起こるかを見ていきます。
- コード 5 行目:2 つの変数 x、y =8、4 を定義します
- コード 7 行目:Python の if ステートメントは、条件 x
False であることを確認します。 この場合 - コード 8 行目:変数 st は NOT です 「x は y より小さい」に設定
- コード 9 行目:行 print st – は、宣言されていない変数の値を出力しようとしています。したがって、エラーが発生します。
「else 条件」の使い方
「else 条件」は通常、あるステートメントを他のステートメントに基づいて判断する必要がある場合に使用されます。 1 つの条件がうまくいかない場合は、ステートメントまたはロジックを正当化する別の条件が存在する必要があります。
例 :
# #Example file for working with conditional statement # def main(): x,y =8,4 if(x < y): st= "x is less than y" else: st= "x is greater than y" print (st) if __name__ == "__main__": main()
- コード 5 行目:2 つの変数 x、y =8、4 を定義します
- コード 7 行目:Python の if ステートメントは、条件 x
False であることを確認します。 この場合 - コード 9 行目:プログラム制御の流れは else 条件に移行します
- コード 10 行目:変数 st が「x is greater」に設定されています より」
- コード 11 行目:行 print st は変数 st の値を出力します。これは「x は y より大きい」です。
「else条件」がうまくいかない場合
「else 条件」では期待した結果が得られない場合が多くあります。プログラムロジックに誤りがあるため、間違った結果が出力されます。ほとんどの場合、これは、プログラム内で 3 つ以上のステートメントまたは条件を正当化する必要がある場合に発生します。
例 この概念を理解するのに役立ちます。
ここでは、両方の変数が同じ (8,8) で、プログラムの出力は 「x は y より大きい」 です。 間違っている .これは、最初の条件 (Python の if 条件) をチェックし、失敗した場合は 2 番目の条件 (else 条件) をデフォルトとして出力するためです。次のステップでは、このエラーを修正する方法を見ていきます。
# #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" else: st= "x is greater than y" print(st) if __name__ == "__main__": main()
「elif」条件の使い方
「else 条件」による以前のエラーを修正するには、「elif」 を使用できます。 声明。 「エリフ」を使用して 」条件、プログラムに 3 番目の条件を出力するか、他の条件が間違っているか正しくない場合の可能性を出力するように指示しています。
例
# #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" elif (x == y): st= "x is same as y" else: st="x is greater than y" print(st) if __name__ == "__main__": main()
- コード 5 行目:2 つの変数 x、y =8、8 を定義します
- コード 7 行目:if ステートメントは条件 x
False であることを確認します この場合 - コード 10 行目:プログラム制御の流れは、elseif 条件に進みます。 x==y が true かどうかをチェックします
- コード 11 行目:変数 st が「x は same as 」に設定されています はい。」
- コード 15 行目:プログラム制御の流れは if ステートメントを終了します (else ステートメントには到達しません)。 そして、変数 st を出力します。出力は正しい「x is same as y」です
最小限のコードで条件文を実行する方法
このステップでは、条件文を要約する方法を見ていきます。条件ごとに個別にコードを実行する代わりに、単一のコードでそれらを使用できます。
構文
A If B else C
例 :
def main(): x,y = 10,8 st = "x is less than y" if (x < y) else "x is greater than or equal to y" print(st) if __name__ == "__main__": main()
- コード 2 行目:2 つの変数 x、y =10、8 を定義します
- コード 3 行目:変数 st が「x は y より小さい」に設定されます。「x
y 変数では、st が 「x は y 以上」 に設定されています。 - コード 4 行目:st の値を出力し、正しい出力を提供します
- 条件文の長いコードを書く代わりに、Python では短く簡潔な方法で自由にコードを書くことができます。
Python ネストされた if ステートメント
次の例は、ネストされた if ステートメント Python を示しています
total = 100 #country = "US" country = "AU" if country == "US": if total <= 50: print("Shipping Cost is $50") elif total <= 100: print("Shipping Cost is $25") elif total <= 150: print("Shipping Costs $5") else: print("FREE") if country == "AU": if total <= 50: print("Shipping Cost is $100") else: print("FREE")
上記のコードの行 2 のコメントを外し、行 3 のコメントを外して、コードを再度実行します
Python の Switch Case ステートメント
Switch ステートメントとは
switch ステートメントは、変数の値を case ステートメントで指定された値と比較する多分岐ステートメントです。
Python 言語には switch ステートメントがありません。
Python は辞書マッピングを使用して Python で Switch Case を実装します
例
function(argument){ switch(argument) { case 0: return "This is Case Zero"; case 1: return " This is Case One"; case 2: return " This is Case Two "; default: return "nothing"; }; };
上記の Python の Switch ケースの場合
def SwitchExample(argument): switcher = { 0: " This is Case Zero ", 1: " This is Case One ", 2: " This is Case Two ", } return switcher.get(argument, "nothing") if __name__ == "__main__": argument = 1 print (SwitchExample(argument))
Python 2 の例
上記のコードは Python 3 の例です。Python 2 で実行する場合は、次のコードを検討してください。
# If Statement #Example file for working with conditional statement # def main(): x,y =2,8 if(x < y): st= "x is less than y" print st if __name__ == "__main__": main() # How to use "else condition" #Example file for working with conditional statement # def main(): x,y =8,4 if(x < y): st= "x is less than y" else: st= "x is greater than y" print st if __name__ == "__main__": main() # When "else condition" does not work #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" else: st= "x is greater than y" print st if __name__ == "__main__": main() # How to use "elif" condition #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" elif (x == y): st= "x is same as y" else: st="x is greater than y" print st if __name__ == "__main__": main() # How to execute conditional statement with minimal code def main(): x,y = 10,8 st = "x is less than y" if (x < y) else "x is greater than or equal to y" print st if __name__ == "__main__": main() # Nested IF Statement total = 100 #country = "US" country = "AU" if country == "US": if total <= 50: print "Shipping Cost is $50" elif total <= 100: print "Shipping Cost is $25" elif total <= 150: print "Shipping Costs $5" else: print "FREE" if country == "AU": if total <= 50: print "Shipping Cost is $100" else: print "FREE" #Switch Statement def SwitchExample(argument): switcher = { 0: " This is Case Zero ", 1: " This is Case One ", 2: " This is Case Two ", } return switcher.get(argument, "nothing") if __name__ == "__main__": argument = 1 print SwitchExample(argument)
まとめ:
Python の条件ステートメントは if ステートメントによって処理されます。ここでは、Python if else のような条件ステートメントを使用できるさまざまな方法を見てきました。
- 「if 条件」 – 条件の 1 つが true または false の場合に結果を出力する必要がある場合に使用します。
- 「else condition」- 1 つの条件が要件を満たしていない場合にステートメントを出力する場合に使用します
- 「elif 条件」 – 結果として 3 番目の可能性がある場合に使用されます。複数の elif 条件を使用して 4 th をチェックできます ,5 ,6 th コードの可能性
- 単一のステートメントですべての条件を宣言してコードを実行することにより、最小限のコードを使用して条件付きステートメントを実行できます
- Python If ステートメントはネスト可能
Python