Python Conditional Statements: if-elif-else, Dictionary, and match-case Explained

1. Introduction

Python is loved by many programmers for its simplicity and intuitive syntax, but some may be surprised to find that it lacks the “switch-case” statement available in other languages. Instead, Python requires the use of “if-elif-else” statements or dictionaries to achieve similar functionality. In this article, we will walk through traditional conditional branching methods in Python, dictionary-based alternatives, and the new “match-case” statement introduced in Python 3.10. By the end, you will master efficient conditional branching in Python and be able to apply it in real-world coding scenarios.

2. Why Python Doesn’t Have a Switch Statement

The reason Python does not include a switch-case statement lies in its simplicity and readability. Python’s designers have always aimed to keep the language concise and eliminate redundant or verbose syntax. While switch statements are useful in other languages, Python achieves the same functionality with “if-elif-else” statements or dictionaries, making switch unnecessary. In fact, Python’s official documentation states that if-elif-else statements are sufficient to cover the functionality of switch. Below is an example of a switch statement in C:
switch (value) {
  case 1:
    printf("Value is 1");
    break;
  case 2:
    printf("Value is 2");
    break;
  default:
    printf("Other value");
}
In Python, this can be rewritten as:
value = 1
if value == 1:
    print("Value is 1")
elif value == 2:
    print("Value is 2")
else:
    print("Other value")
With this approach, Python achieves conditional branching without the need for a switch statement.
RUNTEQ(ランテック)|超実戦型エンジニア育成スクール

3. Using if-elif-else

The most basic way to handle conditional branching in Python is the “if-elif-else” statement. This is highly effective when executing different actions based on multiple conditions. Here is a typical example:
value = 100
if value < 50:
    print("This is a small value")
elif value < 100:
    print("This is a medium value")
else:
    print("This is a large value")
In this code, a different message is displayed depending on the value of the variable value. The if-elif-else statement allows for simple and intuitive conditional branching.

The Problem When Conditions Increase

However, as the nesting of if-elif-else statements grows, the code may become harder to read. Especially when dealing with complex conditions, the dictionary-based method introduced next is highly effective.

4. Conditional Branching with Dictionaries

By using Python’s dictionary, you can implement conditional branching similar to a switch-case statement. A dictionary stores key-value pairs and can express multiple conditions concisely. In the following example, a result is displayed depending on the number entered by the user:
numbers = {1: "One", 2: "Two", 3: "Three"}
value = int(input("Enter a number between 1 and 3: "))

if value in numbers:
    print(f"Selected number: {numbers[value]}")
else:
    print("Please enter a number between 1 and 3")
In this way, dictionaries help keep the code short and readable even when handling many conditions.

Advantages of Using Dictionaries

The dictionary-based approach has the advantage of maintaining readability and simplifying maintenance, even as the number of conditions increases. It is especially efficient when multiple actions are mapped directly to values compared to if-elif-else.

5. New in Python 3.10: The match-case Statement

The “match-case” statement, introduced in Python 3.10, is very similar to a switch-case statement and allows multiple conditions to be written more concisely. This new feature is particularly powerful for pattern matching, greatly improving readability and maintainability compared to if-elif-else or dictionary-based methods.

Basic Syntax of match-case

Here is a basic example of using match-case:
def check_value(value):
    match value:
        case 1:
            print("One was selected")
        case 2:
            print("Two was selected")
        case _:
            print("A value other than 1 or 2 was selected")
In this code, different actions are executed depending on the value of value. The case _ serves as the default action when none of the conditions match.

6. Advanced Examples of match-case

The match-case statement is also well-suited for more complex conditional branching and pattern matching. For example, it can handle lists depending on their length or data type.

Handling Multiple Conditions with match-case

def process_data(data):
    match data:
        case [x, y]:
            print(f"The list contains two elements: {x}, {y}")
        case [x, y, z]:
            print(f"The list contains three elements: {x}, {y}, {z}")
        case _:
            print("The list has a different number of elements")
In this example, the behavior changes depending on the number of elements in the list. The match-case statement makes it easy to describe multiple conditions and patterns concisely.

7. Comparison of if-elif-else, Dictionary, and match-case

Each conditional branching method has its own strengths and weaknesses. The table below shows a comparison:
MethodAdvantagesDisadvantages
if-elif-elseSimple and intuitiveBecomes complex when conditions increase
DictionaryHigh readability, efficient when many conditionsNot all conditions fit into a dictionary
match-caseEffective for multiple conditions and pattern matchingAvailable only in Python 3.10 and later
This comparison makes it easier to choose the most suitable conditional branching method. In particular, match-case is highly effective for complex pattern matching and helps avoid the redundancy of if-elif-else statements.

8. Conclusion

Although Python lacks a switch-case statement, conditional branching can be effectively handled using if-elif-else, dictionaries, and the match-case statement introduced in Python 3.10. The match-case statement is especially strong for pattern matching, allowing complex conditions to be written more simply and becoming a valuable tool in future development. By understanding each method, you can select the most appropriate approach for your project and write efficient, readable code.