Until Python 3.10, there was no feature equivalent to the switch
statement found in other programming languages.
As a result, to execute multiple conditional statements, you would have had to use the elif
keyword, like this:
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.
Starting from Python 3.10, a new feature called “structural pattern matching” was introduced, which functions like the switch
statement found in other programming languages. This feature is implemented using the match
and case
keywords.
There is some debate over whether match
and case
are truly keywords in Python, as they can also be used as variable and function names. But that’s a topic for another time.
You can consider both of these as “soft keywords.”
In this article, I will guide you through how to write a switch
-like statement in Python using the match
and case
keywords.
Before that, however, I’ll explain how Python developers used to simulate a switch
statement in earlier versions.
How Python Programmers Used to Simulate Switch Case
In the past, Python developers used various methods to simulate switch
statements.
One method was using a function along with the elif
keyword, which you can implement like this:
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
"""
How to Implement Switch Statements with the match
and case
Keywords in Python 3.10
To write switch statements using the structural pattern matching feature, you can use the following syntax:
match term:
case pattern-1:
action-1
case pattern-2:
action-2
case pattern-3:
action-3
case _:
action-default
Note that the underscore symbol is used to define the default case in a Python switch statement.
Here’s an example of a switch statement written using the match
–case
syntax. The program prints what you can become by learning different programming languages:
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.")
This syntax is much cleaner than using multiple elif
statements or simulating a switch statement with a function.
You may have noticed that I didn’t include a break
keyword in each case, as is common in other programming languages. This is an advantage of Python’s native switch statement—the functionality of the break
keyword is handled automatically behind the scenes.
Conclusion
In this article, you learned how to write switch statements using the match
and case
keywords. You also saw how Python developers used to implement them before version 3.10.
The match
and case
statements in Python were introduced to offer the same functionality as the switch
statement found in other programming languages like JavaScript, PHP, C++, and more.
Thank you for reading!