Sign Up

Sign In

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Please make a payment to be able to ask a question.

Pay to ask a question

Please make a payment to be able to add post.

Pay to add a post

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Simplify Your Code with Python’s Switch Statement: A Case Study

In other programming languages, the switch statement is implemented by a feature in Python that was not available until version 3.10.

In order to execute multiple conditional statements, you would have used the elif keyword as follows:


age = 120

if age > 90:
    print("You are too old to party, granny.")
elif age < 0:
    print("You're yet to be born")
elif age >= 18:
    print("You are allowed to party")
else: 
    "You're too young to party"

# Output: You are too old to party, granny.

Using the match and case keywords, you can implement Python’s switch case feature called structural pattern matching.

Since both match and case can be used as variable and function names, some people debate whether or not they are keywords in Python.

Both keywords can be considered “soft keywords”.

Using the match and case keywords, I will show you how to write a switch statement in Python.

Before that, let me show you how Python programmers used to simulate switch statements.

Python Programmers’ Method for Simulating Switch Cases

In the days of Pythonistas, switch statements could be simulated in a variety of ways.

You can do this by using a function and the elif keyword as follows:


def switch(lang):
    if lang == "JavaScript":
        return "You can become a web developer."
    elif lang == "PHP":
        return "You can become a backend developer."
    elif lang == "Python":
        return "You can become a Data Scientist"
    elif lang == "Solidity":
        return "You can become a Blockchain developer."
    elif lang == "Java":
        return "You can become a mobile app developer"

print(switch("JavaScript"))   
print(switch("PHP"))   
print(switch("Java"))  

"""
Output: 
You can become a web developer.
You can become a backend developer.
You can become a mobile app developer
"""

Python 3.10 Switch Statements with match and case keywords

Using the structural pattern matching feature, you can write switch statements as follows:


match term:
    case pattern-1:
         action-1
    case pattern-2:
         action-2
    case pattern-3:
         action-3
    case _:
        action-default

In Python, the underscore symbol defines a default case for the switch statement.

The following is an example of a switch statement written with the match case syntax. When you learn various programming languages, you can become:


lang = input("What's the programming language you want to learn? ")

match lang:
    case "JavaScript":
        print("You can become a web developer.")

    case "Python":
        print("You can become a Data Scientist")

    case "PHP":
        print("You can become a backend developer")
    
    case "Solidity":
        print("You can become a Blockchain developer")

    case "Java":
        print("You can become a mobile app developer")
    case _:
        print("The language doesn't matter, what matters is solving problems.")

That’s a much cleaner syntax than using multiple elif statements and simulating the switch statement with a function.

Python’s native switch statement has the advantage over other programming languages in that the break keyword’s functionality is done for you.

Conclusion

In this article, you learned how Python programmers used to write switch statements before version 3.10.

Python’s match and case statements provide the functionality of the switch statement in other programming languages such as JavaScript, PHP, C++, and others.

Related Posts

Leave a comment