Switch Case in Python

Switch-case statements can be used in most programming languages. Following is a generic flowchart for a switch-case statement:

Start
|
V
Read input value
|
V
Switch (value)
|
V
Case 1:
    Perform action 1
    Break
|
Case 2:
    Perform action 2
    Break
|
Case 3:
    Perform action 3
    Break
|
Default:
    Perform default action
    Break
|
End

This flowchart starts with reading the input value and then switches on the value to perform different actions based on the case. The cases are listed as Case 1, Case 2, Case 3, and so on, and the actions to be taken in each case are listed below. If the value doesn’t match any of the cases, the default action is taken. The Break statement is used to terminate the switch-case statement after the appropriate action has been taken.

Python doesn’t have a built-in switch-case statement like some other programming languages. However, there are a few different ways to achieve similar functionality in Python. Here are a few options:

Option 1: Using if-elif-else statements You can use if-elif-else statements to achieve similar functionality to a switch-case statement. Here’s an example:

def switch_case(value):
    if value == 1:
        print("One")
    elif value == 2:
        print("Two")
    elif value == 3:
        print("Three")
    else:
        print("Invalid value")

switch_case(2) # prints "Two"

In this example, the switch_case function takes a value as an argument and uses if-elif-else statements to check the value and perform different actions based on the value.

Option 2: Using dictionaries You can also use dictionaries to achieve similar functionality to a switch-case statement. Here’s an example:

def switch_case(value):
    case_dict = {
        1: "One",
        2: "Two",
        3: "Three"
    }
    print(case_dict.get(value, "Invalid value"))

switch_case(2) # prints "Two"

In this example, the switch_case function uses a dictionary to map different values to different actions. The get method is used to retrieve the value from the dictionary, and if the value is not found, it returns a default value of “Invalid value”.

Option 3: Using classes you can create a class with methods to achieve similar functionality to a switch-case statement. Here’s an example:

class SwitchCase:
    def case1(self):
        print("One")
        
    def case2(self):
        print("Two")
        
    def case3(self):
        print("Three")
        
    def default(self):
        print("Invalid value")

def switch_case(value):
    switch = SwitchCase()
    getattr(switch, "case" + str(value), switch.default)()

switch_case(2) # prints "Two"

In this example, the SwitchCase class has methods to handle different cases. The switch_case function uses the getattr function to dynamically call the appropriate method based on the value. If the value doesn’t match any of the cases, the default method is called.

While Python doesn’t have a built-in switch-case statement, these are some ways you can achieve similar functionality.