Created
February 8, 2015 19:55
-
-
Save jumbojet/182d139ba32dcd73fa57 to your computer and use it in GitHub Desktop.
switch case in python
This file contains hidden or 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
'''Using the if else construct ''' | |
def function_if_else(condtion_variable): | |
if condtion_variable = -1: | |
print "Variable is equal to minus 1" | |
elif condtion_variable == 0: | |
print "Variable is equal to 0" | |
else condtion_variable =1: | |
print "Variable is equal to 1" | |
'''We can use dictionary ''' | |
def function_switch_like(condtion_variable): | |
try: | |
print { | |
-1: "Variable is equal to minus 1", | |
0: "Variable is equal to 0", | |
1:"Variable is equal to 1" | |
}[condtion_variable] | |
except KeyError: | |
print "Key does not exists" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment