Where is the Python switch statement?
The switch statment #
The switch statement is a control flow statement that allows you to execute different blocks of code based on the value of a variable. It is commonly used in many programming languages, such as C, C++, and Java.
It is useful when you want to execute different blocks of code based on the value of a variable.
However, Python does not have a built-in switch statement.
for example in Java it looks like this:
switch (variable) {
case value1:
// code block for value1
break;
case value2:
// code block for value2
break;
default:
// code block if no cases match
}
Use match-case if you can #
In Python 3.10 and later, you can use the match-case statement, which is similar to a switch statement. You are basically using a switch statement but with a different name.
variable = "value1"
match variable:
case "value1":
# code block for value1
case "value2":
# code block for value2
case _:
# _ is a wildcard that matches anything, acting as a default case
# code block if no cases match
match has some powerful features
The match-case statement also has some powerful features that allows you to match complex patterns, such as lists, dictionaries, and even classes.
Here is an example of using match-case to match a list and use the unpacking operator *
to get the rest of the list:
def multiple(numbers):
result = 1
for number in numbers:
result *= number
return result
command = ["add", 2, 3, 2]
match command:
case ["add", *numbers]:
# process the list of numbers
result = 0
for number in numbers:
result += number
print("result", result)
case ["mult", *numbers]:
# or call a function with the list of numbers
print("result", multiple(numbers))
# output
# result 7
For more examples of what you can do with match-case, check out the Python match documentation , and the PEP 636 – Structural Pattern Matching Tutorial .
Pros: Easily readable and maintainable, especially for complex conditions and similar to a switch statement.
Cons: Only available in Python 3.10 and later.
Using if-elif-else #
If you are using an older version of Python , you can use if-elif-else statements to achieve similar functionality. This is common approach and is fine for a small number of cases.
variable = "value1"
if variable == "value1":
# code block for value1
elif variable == "value2":
# code block for value2
else:
# code block if no cases match
Pros: Simple and easy to understand.
Cons: Not scalable, as the number of if statements increases, the code becomes harder to read and maintain.
Use dictionaries #
This is a good option if you have a large number of cases and want to avoid long if-elif-else chains. You can use a dictionary to map values to functions. Since the value of the dictionary is a function, you can call it directly.
def mult(a, b):
return a * b
def add(a, b):
return a + b
# ... add() ... subtract() ...
def default_case(a, b):
raise NotImplementedError
switch_dict = {
"mult": mult,
"add": add
}
variable = "mult"
res = switch_dict.get(variable, default_case)(2, 3)
print(res)
Alternatively, if your functions are simple, you can replace them with inline lambdas.
switch_dict = {
"mult": lambda a, b: a * b,
"add": lambda a, b: a + b
}
variable = "mult"
res = switch_dict.get(variable, lambda a, b: raise NotImplementedError)(2, 3)
print(res)
Pros: Scalable and easy to read, especially for a large number of cases.
Cons: Less readable for simple cases where only a few conditions are required. Also, the default case is not as clear as in a switch statement.