Python | Implementing switch case using functions

Let us take an example to implement the switch case in Python.

Method 1: Using case

print("You can choose from the following language")
print("1. JavaScript\n2. Python\n3. PHP\n4. Solidity\n5. Java\n6. Other \n ")
print(">>NOTE: Your input is case sensitive. Enter first letter in capital<< \n  ")
prog_language = input("What's the programming language you want to learn? ")

match prog_language:
    case "JavaScript":
        print("You can become a web developer.")

    case "Python":
        print("You can become a Data Scientist")

    case "PHP":
        print("You can become a backend developer")
    
    case "Solidity":
        print("You can become a Blockchain developer")

    case "Java":
        print("You can become a mobile app developer")
    case _:
        print("The language doesn't matter, what matters is solving problems.")

Output:

You can choose from the following language
1. JavaScript
2. Python
3. PHP
4. Solidity
5. Java
6. Other 
 
>>NOTE: Your input is case sensitive. Enter first letter in capital<< 
  
What's the programming language you want to learn? Solidity
You can become a Blockchain developer

What's the programming language you want to learn? Javascript
You can become a web developer.

Explanation:

Here we took the input from the user and then passed that input to match it with the different cases. We assigned each case a different programming language and based on that whichever case is called, the code inside that is rendered. Let us take a look at other ways too.

Method 2: Implementing switch case using if-else

bike = input("Enter bike name: ")
 
if bike == 'Hero':
    print("The name of bike is Hero")
 
elif bike == "Suzuki":
    print("The name of bike is Suzuki")
 
elif bike == "Yamaha":
    print("The name of bike is Yamaha")
 
else:
    print("Please choose correct answer")

Explanation:

Here we made use of conditionals to make a switch case. Based on the input of the user we match the input of the user with the value corresponding to the if/else statement.

Method 3: Using functions

class switch_func:
	def day(self, month):

		default = "Incorrect day"

		return getattr(self, 'case_' + str(month), lambda: default)()

	def case_1(self):
		return "Jan"

	def case_2(self):
		return "Feb"

	def case_3(self):
		return "Mar"

	def case_4(self):
		return "Apr"
	
	def case_5(self):
		return "May"

switch_case = switch_func()

print(switch_case.day(1))

print(switch_case.day(5))

Method 4: Using switcher

def addition_num(num1, num2):
  num1 += num2
  return num1
def subtraction_num(num1, num2):
  num1 -= num2
  return num1
def mul_num(num1, num2):
  num1 *= num2
  return num1
def division_num(num1, num2):
  num1 /= num2
  return num1
def default(num1, num2):
  return "Incorrect day"
switcher = {
    1: addition_num,
    2: subtraction_num,
    3: mul_num,
    4: division_num,
    
}
def switch(operation, num1, num2):
  return switcher.get(operation, default)(num1, num2)
print('''You can perform operation
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Module ''')
# Take input from user
choice = int(input("Select operation from 1,2,3,4 : "))
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print (switch(choice, num1, num2))

Here is the end of our article. We saw overall 4 methods by which we can use switch cases.

Share:

Author: Dhvanil V