Last active
December 2, 2018 16:12
-
-
Save RDCH106/3872eb287ed55b1c9597111ddb327efd to your computer and use it in GitHub Desktop.
Examples of switch flow control structure in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################# | |
# 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