Python

Python switch statement syntax

A “switch” statement is a control flow statement that selects one of the multiple blocks of code to be executed according to the particular variable value. This statement is used to execute different actions depending on applied conditions. It also has a “default” case that is applied when no other cases are applicable. As an alternative to the “switch” statement, Python provides several other options that will be discussed in this post.

The Syntax of a Switch Statement in Python

Python does not have a “switch” statement in the traditional sense. However, there are a few ways to implement the same functionality using the following approaches:

Method 1: Using “match-case”

Python 3.10 introduced a new feature called “match-case”, which can be used to implement “switch” statements in a more concise and readable way.

Example

The below code is used to implement the “switch” statement in Python:

name = input("What is your name? ")
match name:
  case "Joseph":
    print("Hello, Joseph!")
  case "Anna":
    print("Goodbye, Mary!")
  case _:
    print("Who are you?")

 

The above code checks whether the entered name matches one of the predefined cases. If the name matches, the desired output against the invoked case is printed on the console screen.

Output

Method 2: Using “if/else” Statement

One way to implement a “switch” statement in Python is to use a series of “if/else” statements.

Example

The following code is used to implement the functionality of the “switch” statement in Python:

name = input("What is your name? ")
if name == "Joseph":
  print("Hello, Joseph!")
elif name == "Mary":
  print("Goodbye, Mary!")
else:
  print("Who are you?")

 

In the above code, the “if/else” statement is used in series to execute the program based on the specified conditions.

Output

In this output, it can be seen that the corresponding statement is invoked based on the entered name accordingly.

Method 3: Using “Dictionary”

Another way to implement a switch statement in Python is to use a “Dictionary”. This is a shorter way to do it, but it can be less readable.

Example

The below code is used to implement the “switch” statement using “Dictionary”:

names = {
  "Joseph": "Hello",
  "Anna": "Goodbye",
}
name = input("What is your name? ")
print(names.get(name, "Who are you?"))

 

In the above code, the “Dictionary” is used to initialize the stated values in “key-value” pairs, and the “get()” method is used to get the value of the entered key from the dictionary.

Output

Conclusion

Python does not have switch statements like other languages. However, the “switch” statement in Python can be implemented by using the “match-case” method, the series of “if/else” statements, or the “Dictionary. The article teaches us what a switch-case statement means and how to use the “switch” statement syntax in Python. The switch syntax can help us code better and faster, and it is easy to run.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.