World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Pallavi Dhotre
a year ago
switch(Boolean expression) {
case 1:
// code block1
break;
case 2:
// code block2
break;
default:
// code block
}
#python switch case example using dictionary value string
def language(i):
switch_demo = {
0: 'Python',
1: 'Java',
2: 'C++',
3: 'Ruby',
4: 'C'
}
print( switch_demo.get(i, "Invalid language"))
inp=int(input("Enter your choice: "))
language(inp)
Enter your choice: 0
Python
#python switch case example using dictionary
def Python():
print("I like Python")
def Java():
print("I like Java")
def CPP():
print("I like CPP")
def Ruby():
print("I like Ruby")
def C():
print("I like C")
def language(i):
switch_demo = {
0: Python,
1: Java,
2: CPP,
3: Ruby,
4: C
}
obj=switch_demo.get(i, lambda :"Invalid language")()
return (obj)
i=int(input("Enter your choice: "))
language(i)
Enter your choice: 3
I like Ruby