Python Match Statement Tutorial
The match statement is one of the most useful features introduced in Python 3.10. It works similarly to the switch-case statement available in many programming languages like Java, C#, and JavaScript.
Using the match statement, developers can write cleaner, more readable, and organized conditional logic.
Why Use Match Statement?
- Improves code readability
- Reduces multiple
if-elif-elseconditions - Makes code cleaner and easier to maintain
- Useful for menu-driven applications
- Helpful in automation frameworks and API response handling
Basic Syntax
match variable:
case value1:
# code block
case value2:
# code block
case _:
# default block
Simple Example
day = 3
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case _:
print("Invalid Day")
Output
Wednesday
Understanding the Underscore (_) Case
The underscore _ acts like the default condition.
It executes when none of the cases match.
number = 10
match number:
case 1:
print("One")
case 2:
print("Two")
case _:
print("Number not found")
Output
Number not found
Match Statement with Multiple Values
letter = "a"
match letter:
case "a" | "e" | "i" | "o" | "u":
print("Vowel")
case _:
print("Consonant")
Output
Vowel
Match Statement with User Input
choice = input("Enter your option: ")
match choice:
case "1":
print("Login")
case "2":
print("Logout")
case "3":
print("Profile")
case _:
print("Invalid Option")
Real-Time Example for Automation Testers
Suppose you are validating different environments in an automation framework.
environment = "QA"
match environment:
case "DEV":
print("Running tests on Development Server")
case "QA":
print("Running tests on QA Server")
case "UAT":
print("Running tests on UAT Server")
case "PROD":
print("Running tests on Production Server")
case _:
print("Invalid Environment")
Output
Running tests on QA Server
Difference Between if-elif and match
| if-elif | match-case |
|---|---|
| Long and difficult to manage | Cleaner and more readable |
| Good for complex conditions | Good for fixed value matching |
| Available in all Python versions | Available in Python 3.10+ |
| More repetitive | Less repetitive |
Important Points to Remember
- Match statement works only in Python 3.10 and above.
- Indentation is very important.
- Use
case _as a default block. - Best suited for fixed value comparisons.
- Improves readability in large projects.
When Should You Use Match Statement?
You should use match-case when:
- You have multiple fixed conditions
- You want cleaner code
- You are building menu-based programs
- You are handling API response types
- You are working in automation frameworks
Conclusion
Python's match statement is a powerful feature that simplifies conditional programming. It helps developers write cleaner, maintainable, and professional code.
If you are using Python 3.10 or later, start using the match-case statement in your projects and automation frameworks.
Recommended Practice:
Try converting your existing if-elif-else programs into match-case programs for better understanding.
No comments:
Post a Comment