Match-case statement in Python

Photo by Alev Takil on Unsplash

Match-case statement in Python

·

3 min read

The match statement is a structural pattern that allows for more powerful and concise flow control in your program. In this article, we will explore what the match statement is, how it works, and how you can use it in your own Python programs to write more expressive and maintainable code. The match statement is similar to a switch statement in other programming languages but offers more flexibility and functionality.

Match-case statement syntax

The statement can be initialized with the keyword match, which takes a parameter. The pattern then proceeds with the various cases, using the case keyword and the pattern, waiting for the pattern to match the parameter. Whenever this happens, the code in the case block will be executed. If none of the patterns match the parameter, we make use of a default case that will run when nothing is matched. The pattern of this last case is _.

Here is an example of a match statement:

grade = "B"
match grade:
    case "A":
        print("Impressive!")
    case "B":
        print("Good job!")
    case "C":
        print("A decent result")
    case "D":
        print("You have to improve your score!")
    case "F":
        print("You failed the test")
    case _:
        print("No grade associated, please retry")

# Output => Good job!

In this example, the parameter consists of the variable grade, which is holding the value "B". Once the match statement is initiated, and the parameter is confronted with all the patterns. The parameter is finally confronted with the pattern "B", therefore the code in the case "B" block is executed. If pattern "B" would not have been available, the code inside case _ would have been executed.

Match-case statement and the pipe operator

The pipe operator for the match statement is the equivalent of the logical or operator used in if...else statements. The pipe operator is represented by the character |.

grade = "C"
match grade:
    case "A" | "B":
        print("Well done!")
    case "C" | "D":
        print("You can do better!")
    case "F":
        print("You failed the test")
    case _:   
        print("No grade associated, please retry")

# Output => You can do better!

As for the or operator, the pipe operator returns True if either of the operands evaluates to True. In this example, the grade variable, which is holding the value "C", is confronted with the patterns. The code inside the case "C" | "D" is finally executed, because the parameter matches with one of the patterns in the case.

Conclusion

In conclusion, the match statement is a powerful feature that can help you write more expressive and maintainable code. By allowing you to match parameters and patterns, the match statement can make your code more readable and concise. While the syntax for the match statement may take some time to get used to, it is a worthwhile investment for developers who want to stay up-to-date with the latest Python features. With the match statement in your toolkit, you will be able to tackle complex problems with confidence and clarity.