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

Python if...else ステートメント

Python の if...else ステートメント

この記事では、さまざまな形式の if..else ステートメントを使用して、Python プログラムで決定を作成する方法を学習します。

ビデオ:Python の if...else ステートメント

Python の if...else ステートメントとは?

特定の条件が満たされた場合にのみコードを実行したい場合、意思決定が必要です。

if…elif…else ステートメントは意思決定のために Python で使用されます。

Python if ステートメントの構文

if test expression:
    statement(s)

ここで、プログラムは test expression を評価します テスト式が True の場合にのみステートメントを実行します .

テスト式が False の場合 、ステートメントは実行されません。

Python では、if の本体 ステートメントはインデントで示されます。本文はインデントで始まり、インデントされていない最初の行が終わりを示します。

Python はゼロ以外の値を True と解釈します . None そして 0 False と解釈されます .

Python if ステートメントのフローチャート

<図>

例:Python if ステートメント

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")

プログラムを実行すると、出力は次のようになります:

3 is a positive number
This is always printed
This is also always printed.

上記の例では、num > 0 はテスト式です。

if の本体 これが True と評価された場合にのみ実行されます .

変数 num は 3 に等しく、テスト式は真であり、if の本体内のステートメント

変数 num の場合 は -1 に等しく、テスト式は false で、if の本体内のステートメント スキップされます。

print() ステートメントは if の範囲外です ブロック (インデントなし)。したがって、テスト式に関係なく実行されます。


Python if...else ステートメント

if...else の構文

if test expression:
    Body of if
else:
    Body of else

if..else ステートメントは test expression を評価します if の本体を実行します テスト条件が True の場合のみ .

条件が False の場合 、 else の本体 実行されます。ブロックを区切るためにインデントが使用されます。

Python if..else フローチャート

<図>

if...else の例

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")

出力

Positive or Zero

上記の例では、 num の場合 は 3 に等しく、テスト式は真であり、本体は if です。 が実行され、body の else はスキップされます。

If num は -5 に等しく、テスト式は false で、本体は else です。 が実行され、 if の本体 スキップされます。

If num は 0 に等しく、テスト式は真であり、本体は if です 実行され、body の else はスキップされます。


Python の if...elif...else ステートメント

if...elif...else の構文

if test expression:
    Body of if
elif test expression:
    Body of elif
else: 
    Body of else

elif else if の略です。複数の式をチェックできます。

if の条件が False です 、次の elif の状態をチェックします ブロックなど。

すべての条件が False の場合 、else の本体が実行されます。

いくつかの if...elif...else のうちの 1 つのブロックのみ ブロックは条件に従って実行されます。

if ブロックは else を 1 つだけ持つことができます ブロック。ただし、複数の elif を持つことができます ブロックします。

if...elif...else のフローチャート

<図>

if...elif...else の例

'''In this program, 
we check if the number is positive or
negative or zero and 
display an appropriate message'''

num = 3.4

# Try these two variations as well:
# num = 0
# num = -4.5

if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

変数 num の場合 正、 正の数

If num 0、 ゼロ に等しい

If num 負、 負の数


Python のネストされた if ステートメント

if...elif...else を持つことができます 別の if...elif...else 内のステートメント 声明。これは、コンピューター プログラミングでは入れ子と呼ばれます。

これらのステートメントは、いくつでも互いに入れ子にすることができます。インデントは、入れ子のレベルを把握する唯一の方法です。混乱を招く可能性があるため、必要でない限り避ける必要があります。

ネストされた Python の例

'''In this program, we input a number
check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement'''

num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

出力 1

Enter a number: 5
Positive number

アウトプット 2

Enter a number: -1
Negative number

アウトプット 3

Enter a number: 0
Zero

Python

  1. C# if, if...else, if...else if およびネストされた if ステートメント
  2. Python ステートメント、インデント、およびコメント
  3. Python データ型
  4. Python 演算子
  5. Python pass ステートメント
  6. Python 関数の引数
  7. Python 辞書
  8. try、except、finally ステートメントを使用した Python 例外処理
  9. Python イテレータ
  10. Python クロージャー
  11. Python Print() ステートメント:例を使用して印刷する方法