Python 条件文: IF…Else、ELIF、Switch Case
条件文とは何か Python?
条件文 Python 特定のブール制約が真か偽かに応じて異なる計算やアクションを実行します。条件文はIF文によって処理されます。 Python.
何ですか 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 = 8、XNUMX を定義します。
- コード行7: if文 Python 条件xをチェックする ◯ この場合
- コード行 8: 変数 st は「x は y 未満」に設定されます。
- コード行 9: print st 行は、「x は y より小さい」という変数 st の値を出力します。
「if 条件」が満たされない場合はどうなるか
このステップでは、if条件が成立した場合に何が起こるかを見ていきます。 Python 満たさない。
- コード行 5: 8 つの変数 x、y = 4、XNUMX を定義します。
- コード行7: if文 Python 条件xをチェックする × この場合
- コード行 8: 変数 st は NOT 「x は y 未満」に設定します。
- コード行 9: print st という行は、宣言されていない変数の値を出力しようとしています。 したがって、エラーが発生します。
「else条件」の使い方
「else 条件」は通常、あるステートメントを他のステートメントに基づいて判断する必要がある場合に使用されます。 XNUMX つの条件が間違っている場合、ステートメントまたはロジックを正当化する別の条件が存在する必要があります。
例::
# #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: 8 つの変数 x、y = 4、XNUMX を定義します。
- コード行7: if文 Python 条件xをチェックする × この場合
- コード行 9: プログラム制御の流れは else 条件に進みます。
- コード行 10: 変数 st が「x は 大きい あなたよりも。」
- コード行 11: print st 行は、「x が y より大きい」という変数 st の値を出力します。
「else条件」が機能しない場合
「else 条件」では望ましい結果が得られない場合が多くあるかもしれません。 プログラムロジックに誤りがあるため、間違った結果が出力されます。 ほとんどの場合、これはプログラム内で XNUMX つ以上のステートメントまたは条件を正当化する必要がある場合に発生します。
An 例 この概念をよりよく理解するのに役立ちます。
ここで、両方の変数は同じ (8,8) であり、プログラムの出力は次のようになります。 「x は y より大きい」、 どちらである WRONGこれは、最初の条件( Python)、失敗した場合は、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」条件を使用すると、他の条件が間違っているか正しくない場合に XNUMX 番目の条件または可能性を出力するようにプログラムに指示していることになります。
例:
# #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: 8 つの変数 x、y = 8、XNUMX を定義します。
- コード行 7: if ステートメントは条件 x をチェックします。 × この場合
- コード行 10: プログラム制御の流れは elseif 条件に進みます。 x==y が true かどうかをチェックします
- コード行 11: 変数 st が「x は と同じ 」
- コード行 15: プログラム制御の流れは if ステートメントを終了します (else ステートメントには到達しません)。 そして変数 st を出力します。 出力は「x は 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: 10 つの変数 x、y = 8、XNUMX を定義します。
- コード行 3: x の場合、変数 st は「x は y 未満」に設定されます。 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 をコメントにして、コードを再度実行します。
Switch Caseステートメント Python
Switchステートメントとは何ですか?
switch ステートメントは、変数の値を case ステートメントで指定された値と比較する多方向分岐ステートメントです。
Python 言語には switch ステートメントがありません。
Python 辞書を使用する Switch Caseを実装するためのマッピング Python
例:
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";
};
};
上記のSwitchケースの場合 Python
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 条件」 – いずれかの条件が true または false の場合に結果を出力する必要がある場合に使用されます。
- 「else 条件」 - XNUMX つの条件が要件を満たさない場合にステートメントを出力する場合に使用します。
- 「elif 条件」 – 結果として 4 番目の可能性がある場合に使用されます。 複数の elif 条件を使用して XNUMX つをチェックできます。th,5th,6th コードの可能性
- すべての条件を XNUMX つのステートメントで宣言してコードを実行することで、最小限のコードを使用して条件ステートメントを実行できます。
- Python If文はネストできる







