工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> Python

Python の条件文:IF…Else、ELIF、Switch Case

Python の条件文とは?

Python の条件付きステートメントは、特定のブール制約が true または false に評価されるかどうかに応じて、異なる計算またはアクションを実行します。条件ステートメントは、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()

「if 条件」が満たされない場合

このステップでは、Python の if 条件が満たされない場合に何が起こるかを見ていきます。

「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()

「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()

最小限のコードで条件文を実行する方法

このステップでは、条件文を要約する方法を見ていきます。条件ごとに個別にコードを実行する代わりに、単一のコードでそれらを使用できます。

構文

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

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 のような条件ステートメントを使用できるさまざまな方法を見てきました。


Python

  1. C# if, if...else, if...else if およびネストされた if ステートメント
  2. C# switch ステートメント
  3. C++ switch..case ステートメント
  4. C if...else ステートメント
  5. C スイッチステートメント
  6. Python ステートメント、インデント、およびコメント
  7. Python if...else ステートメント
  8. Python pass ステートメント
  9. Java if...else ステートメント
  10. C++ Switch Case ステートメントと EXAMPLE
  11. Python Print() ステートメント:例を使用して印刷する方法