Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Master Python Conditional Statements: IF, ELSE, ELIF, and Switch‑Like Logic

What Are Conditional Statements in Python?

Conditional statements enable a program to execute different blocks of code based on Boolean expressions. In Python, the if statement is the core construct that drives this decision‑making process.

In this guide, we’ll explore how to use if, else, elif, and Python’s dictionary‑based switch‑case alternative. Each section includes concise examples to illustrate the concepts.

Python if Statement Basics

Syntax

if expression:
    # block executed when expression is True
else:
    # block executed when expression is False

Example:

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

What Happens When the if Condition Is False?

If the condition evaluates to False and no else clause is provided, the body of the if is skipped and the program continues after the block.

Example with a failing condition:

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

Using the else Clause

The else block runs when the preceding if condition is False. It is useful when you have a binary decision.

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

Common Mistakes with else

When two values are equal, an else block may incorrectly report a comparison as “greater” or “less.”

def main():
    x, y = 8, 8
    if x < y:
        st = "x is less than y"
    else:
        st = "x is greater than y"  # Incorrect when x == y
    print(st)

if __name__ == "__main__":
    main()

Introducing elif for Multiple Branches

The elif clause allows you to test additional conditions after the initial if. It prevents the fall‑through error seen above.

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

Condensing Logic with a Ternary Expression

Python’s one‑liner syntax can make simple comparisons concise:

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 Statements

Nested conditions are useful when a decision depends on multiple variables.

total = 100
country = "AU"  # Change to "US" to test the other block

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

Switch‑Case Equivalent in Python

Python lacks a native switch statement, but a dictionary lookup offers the same clarity and efficiency.

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

Summary

Python

  1. Mastering the C# Switch Statement: Syntax, Examples & Best Practices
  2. Mastering the C++ Switch‑Case Statement: Syntax, Workflow, and Practical Examples
  3. Mastering Python If…Else Statements
  4. Mastering the Python Pass Statement: A Practical Guide
  5. Mastering the Java Switch Statement: Syntax, Usage, and Best Practices
  6. Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
  7. Mastering C Conditional Statements: IF, IF-ELSE, and Nested IF-ELSE Explained
  8. Understanding Switch‑Case in C: Syntax, Examples, and Best Practices
  9. Java Switch‑Case Statement Explained: Syntax, Examples, and Best Practices
  10. Python Decision Making: Mastering Conditional Logic