Switch

Switch is a control mechanism used to allow the value of a variable or expression to change the control flow of program execution.

Python Example

Python doesn’t support a native switch function. The example below is one alternative implementation.

# Define a variable to control the switch.
switch_control = 2

# Define the switch logic to execute selected functions depending on the switch_control variable.
if switch_control == 1:
    function_1( )
elif switch_control == 2:
    function_2( )
else:
    function_none( )

# Define the functions referenced in the switch.
def function_1( ):
    print("function_1")

def function_2():
    print("function_2")