Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

switch case dictionary python

result = {
  'a': lambda x: x * 5,
  'b': lambda x: x + 7,
  'c': lambda x: x - 2
}[value](x)
Comment

Python Switch case statement by Dictionary Mapping

# Implement Python Switch Case Statement using Dictionary

def monday():
    return "monday"
def tuesday():
    return "tuesday"
def wednesday():
    return "wednesday"
def thursday():
    return "thursday"
def friday():
    return "friday"
def saturday():
    return "saturday"
def sunday():
    return "sunday"
def default():
    return "Incorrect day"

switcher = {
    1: monday,
    2: tuesday,
    3: wednesday,
    4: thursday,
    5: friday,
    6: saturday,
    7: sunday
    }

def switch(dayOfWeek):
    return switcher.get(dayOfWeek, default)()

print(switch(3))
print(switch(5))
Comment

PREVIOUS NEXT
Code Example
Python :: python get type of variable 
Python :: tensorflow neural network 
Python :: iterrrows 
Python :: pandas bins dummy 
Python :: np.array([(1,2),(3,4)],dtype 
Python :: how to get only one column from dataset in python 
Python :: python regex match 
Python :: difference between set and list in python 
Python :: remove common rows in two dataframes pandas 
Python :: print colors in python 
Python :: python why call super(class).__init__() 
Python :: Append a line to a text file using the write() function 
Python :: python with 
Python :: how to run a command in command prompt using python 
Python :: python tableau 
Python :: append dictionary python 
Python :: how to chang your facebook name 
Python :: Python get first element from list 
Python :: iterating over tuples in python 
Python :: promises in python 
Python :: replace in lists python 
Python :: rename rows pandas based on condiions 
Python :: Math Module degrees() Function in python 
Python :: python check characters in utf 8 
Python :: python console install package 
Python :: tkinter add text to canvas 
Python :: how to run python in atom 
Python :: remove toggle/pandaslux 
Python :: how to check if object is of file type in python3 
Python :: change a color on touch roblox 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =