Skip to content

Instantly share code, notes, and snippets.

@RDCH106
Last active December 2, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RDCH106/3872eb287ed55b1c9597111ddb327efd to your computer and use it in GitHub Desktop.
Save RDCH106/3872eb287ed55b1c9597111ddb327efd to your computer and use it in GitHub Desktop.
Examples of switch flow control structure in Python
#############################
# Example with values
#############################
def switch_value(x):
return{
0: "APPLE",
1: "ORANGE",
2: "BANANA",
3: "PEACH"
}.get(x, None) # default if x not found
print(switch_value(2)) # Print banana
#############################
#############################
# Example with functions
#############################
def sayHello():
print("Hello")
def sayBye():
print("Bye")
def sayName():
print("Rubén")
def sayNothing():
print("")
def switch_function(x):
return{
0: sayHello,
1: sayBye,
2: sayName,
3: sayNothing
}.get(x, sayNothing) # default if x not found
switch_function(2)() # Print Rubén
#############################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment