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

Python条件分岐完全ガイド:if、else、elif、switch風ロジック

Pythonにおける条件文とは

条件文は、ブール式に基づいて異なるコードブロックを実行することを可能にします。Pythonでは、if文がこの意思決定プロセスを駆動する核心的な構造です。

このガイドでは、if、else、elif、そしてPythonの辞書ベースのswitch-case代替手法の使い方を探ります。各セクションには概念を説明する簡潔な例が含まれます。

Python if 文の基本

構文

if expression:
    # expressionがTrueのとき実行されるブロック
else:
    # expressionがFalseのとき実行されるブロック

例:

def main():
    x, y = 2, 8
    if x < y:
        st = "x is less than y"
    else:
        st = "x is not less than y"
    print(st)
if __name__ == "__main__":
    main()

if 条件が偽のときの動作

条件がFalseと評価され、else節がない場合、ifの本体はスキップされ、ブロック以降のプログラムが継続します。

失敗する条件の例:

def main():
    x, y = 8, 4
    if x < y:
        st = "x is less than y"
    else:
        st = "x is greater than or equal to y"
    print(st)
if __name__ == "__main__":
    main()

else 節の使用

elseブロックは、直前のif条件がFalseのときに実行されます。二者択一の決定に有用です。

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 のよくある間違い

2つの値が等しい場合、elseブロックが「より大きい」や「より小さい」と誤って報告する可能性があります。

def main():
    x, y = 8, 8
    if x < y:
        st = "x is less than y"
    else:
        st = "x is greater than y" # x == yのとき誤り
    print(st)
if __name__ == "__main__":
    main()

elif で複数分岐を導入

elif節を使うと、最初のifの後に追加条件を順番にテストできます。上記のフォールスルーエラーを防ぎます。

def main():
    x, y = 8, 8
    if x < y:
        st = "x is less than y"
    elif x == y:
        st = "x is the same as y"
    else:
        st = "x is greater than y"
    print(st)
if __name__ == "__main__":
    main()

三項式でロジックを凝縮

Pythonのワンライナー構文でシンプルな比較を簡潔に表現:

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

ネストした if 文

ネスト条件は、決定が複数の変数に依存する場合に有用です。

total = 100
country = "AU" # "US"に変更して他ブロックをテスト
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")
elif country == "AU":
    if total <= 50:
        print("Shipping Cost is $100")
    else:
        print("FREE")

PythonでのSwitch-Case相当

Pythonにはネイティブなswitch文がありませんが、辞書ルックアップで同じ明確さと効率を得られます。

def switch_example(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(switch_example(argument))

まとめ

Python

  1. C# 条件分岐をマスターする:if、if-else、if-else if、ネストした if 文
  2. C# switch 文をマスターする:構文、例、ベストプラクティス
  3. C++ switch-case文完全ガイド:構文、フロー、実践的な計算機例
  4. C言語条件文マスター:if、else、ネストの実践ガイド
  5. C言語switch文完全マスター:構文、フロー、実践例
  6. C++ switch文をマスター:構文、使い方、実践例
  7. Python 文・インデント・コメント: 明確なガイド
  8. Python if…else文完全マスター
  9. Python pass文完全マスター: 実践ガイド
  10. Python print()関数の実践ガイド:基本から応用まで
  11. Java if…elseをマスター:制御フローを徹底解説