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 :: how to iterate row wise using 2d integer array in python 
Python :: python sleep command 
Python :: from a list of lists - find all length of list 
Python :: Python NumPy tile Function Example 
Python :: how to overlap two barplots in seaborn 
Python :: insert blank row in data frame 
Python :: server in python 
Python :: if statement python explained 
Python :: change value in tuple 
Python :: max value of a list prolog 
Python :: is there a null data type in python 
Python :: python check empty string 
Python :: command for python shell 
Python :: upload file setup django url 
Python :: explode function in python 
Python :: docker remote 
Python :: title() in python 
Python :: python list comprehensions 
Python :: python strip function 
Python :: List Get a Element 
Python :: summing all Odd Numbers from 1 to N 
Python :: Dynamic Form Fields Django 
Python :: pandas take entries from other column if column is nan 
Python :: iterating string in python 
Python :: test pypi 
Python :: vector data 
Python :: merge sort in python 
Python :: python copy vs deepcopy 
Python :: matplotlib matshow log scale 
Python :: python unbound variable 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =