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.
- Understanding the
ifstatement - When an
ifcondition fails - Using the
elseclause - Common pitfalls with
else - Employing
eliffor multiple branches - Condensing logic with ternary expressions
- Nested
ifstatements - Implementing a switch‑case pattern with dictionaries
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
- if – Executes when a condition is true.
- else – Executes when the preceding
ifis false. - elif – Adds additional Boolean checks in sequence.
- One‑liner (ternary) expressions shorten simple comparisons.
- Nested
ifblocks handle multi‑dimensional decisions. - Dictionary lookups provide a clean switch‑case alternative.
Python
- Mastering the C# Switch Statement: Syntax, Examples & Best Practices
- Mastering the C++ Switch‑Case Statement: Syntax, Workflow, and Practical Examples
- Mastering Python If…Else Statements
- Mastering the Python Pass Statement: A Practical Guide
- Mastering the Java Switch Statement: Syntax, Usage, and Best Practices
- Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
- Mastering C Conditional Statements: IF, IF-ELSE, and Nested IF-ELSE Explained
- Understanding Switch‑Case in C: Syntax, Examples, and Best Practices
- Java Switch‑Case Statement Explained: Syntax, Examples, and Best Practices
- Python Decision Making: Mastering Conditional Logic