Did you know you can write Switch statements in Python?
Learn how in 5 minutes with code examples.

Switch statements are massively popular alternatives to the if-else paradigm and is available in almost all major languages such as C++, Java, and JavaScript. However, it had been missing from Python for a long time.Â
With the version 3.10+, Python now supports switch statements by introducing a new keyword called:Â match
.Â
Let’s dive in to see how it works 👇
The Syntax and BasicsÂ
Fundamentally, it looks and writes similar to switch
 statements:Â
match command:
case pattern:
# do something
case pattern2:
# do something else
case other:
# no matches in all cases
We have the following components:
aÂ
command
 variable to match,Âindividual cases defined with theÂ
case
 keyword, and finally,theÂ
other
 keyword to do something when none of the cases match.Â
Note: TheÂ
case other
 is equivalent to anÂelse
 in theÂif-elif-else
 statement block and can be more simply written asÂcase _,Â
where the _ is a wildcard entry to support any pattern not defined explicitly in the individual cases.Â
Let’s look at a simple example, shall we?Â
command = "Macbeth"
match command:
case 'Macbeth':
print('Shakespeare!')
case 'Emma':
print('Austen')
case 'Anna Karenina':
print('Tolstoy')
case other:
print("Author not found!")
The output is as follows:Â
# Out:
Shakespeare!
Perfect! Now that you know about the basics of this new statement, let’s look at a few other examples of its advanced use cases.Â
Advanced Pattern Matching
You may often want to match more than one pattern in a single case.Â
This can be done with Python’s pipe pattern:Â
match command:
case 'Macbeth' | 'As You Like It' | 'Othello':
print('Shakespeare!')
case 'Emma' | 'Pride and Prejudice':
print('Austen')
case 'Anna Karenina' | 'Resurrection':
print('Tolstoy')
case other:
print("Author not found!")
You can also use tuples to match against combinations of tuples and variables and use if
 conditions to match them with a custom condition on the fly:Â
weather = ("Saturday", 4)
match weather:
case ("Monday", temp) if temp > 30:
print(f"It's too hot on Monday, {temp =}")
case ("Saturday", temp) if temp < 10:
print(f"It's too cold on Saturday, {temp =}")
case (weekday, temp) if temp < 18:
print(f"It's slightly chilly on {weekday=}, {temp =}")
case (weekday, temp) if temp > 25:
print(f"It's hot on {weekday=}, {temp = }")
case _:
raise ValueError("Not a valid weather pattern. Please try again.")
The output will be:Â
# Out:
It's too cold on Saturday, temp = 4
This is one use case. To match any item in a list, you can also use lists in and as case patterns:Â
match command:
case ['Macbeth', 'As You Like It', 'Othello']:
print("Shakespeare!")
case ['Emma', 'Pride and Prejudice']:
print("Austen!")
case _:
print("Author not found!")
You’ll notice that this works just like the pipe (|) command we saw above.Â
Conclusion
Great job on following along!Â
You learned how the matchÂ
statement can provide an advantage over the if-elif-else
 statement paradigm in terms of the amount of code needed to do the same task and the readability of the resulting snippet.
Now that you have an idea of this great addition to Python that helps in writing more concise code, go right ahead and explore more!Â
If you found this article useful, feel free to share it with a friend!