Python Swtich Statement
ASHISH GUPTA
4 min read
- python
Introduction
Are you finding to write switch statements in python because python doesn't have in built switch statement like other programming languages.but it provide many other alternative approach to achieve similar functionality. Switch statements are most common feature in many programming languages, providing a clear code snippet to increase readability and more efficient way to handle multiple conditional branches. So start your vs code or other editor to code because in this blog post we'll learn how to write switch case in different different efficient approaches to write switch cases in python and also we compare with traditional approaches of other languages.
What is a Switch Statement?
A switch statement allows that it run only one block of code through rest of code it means switch case is like run if the case is true if none of case if true then it run default case if the block execute then every time we add break statement to close the block, based on the value of a variable or expression. It's a more readable and efficient way to handle multiple conditions compared to using multiple if-elif-else
statements.
Traditional Switch Statement Example in C
#include <stdio.h>
int main() {
int day = 3;
switch(day) {
case 1:
printf("Pizza\n");
break;
case 2:
printf("Burger\n");
break;
case 3:
printf("Chole Bhature\n");
break;
case 4:
printf("Chai\n");
break;
case 5:
printf("Cofee\n");
break;
default:
printf("Water\n");
}
return 0;
}
above code is written in c language.
Why Python Haven't a in-built Switch Statement?
Python does not have a in built switch statement because Guido van Rossum, who found the Python, he decided against including a switch statement in Python. One of the main reasons is that the if-elif-else
statement is considered as simple and very easy to write code sufficient for most use cases. He thinks why other languages have switch statement because we can use this functionality if-elif-else statement. Additionally, Python have dynamic typing and flexibility offer other ways to achieve similar functionality without needing a dedicated switch statement.
Implementation In python : Using dictionary mapping
Copy of traditionally switch statement in python using dictionary mapping in the code snippet is too similar to c/c++. Here is the code snippet.
*now all code is written in python.
def switch_day(day):
switcher = {
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday"
}
return switcher.get(day, "Weekend")
# Example usage
day = 3
print(switch_day(day))
Implementation In python : Using function dictionary
You can take this approach further by mapping keys to functions, which allows for more complex operations or you can make methods using object oriented programming.
def monday():
return "Monday"
def tuesday():
return "Tuesday"
def wednesday():
return "Wednesday"
def thursday():
return "Thursday"
def friday():
return "Friday"
def default():
return "Weekend"
def switch_day(day):
switcher = {
1: monday,
2: tuesday,
3: wednesday,
4: thursday,
5: friday
}
return switcher.get(day, default)()
# Example usage
day = 3
print(switch_day(day))
Implementation In python : Using Lambda Functions
other approach is to use lambda functions for easy operations:
def switch_day(day):
switcher = {
1: lambda: "Monday",
2: lambda: "Tuesday",
3: lambda: "Wednesday",
4: lambda: "Thursday",
5: lambda: "Friday"
}
return switcher.get(day, lambda: "Weekend")()
# Example usage
day = 3
print(switch_day(day))
Implementation In python : Using if-elif-else Construct
Although less graceful , the if-elif-else
construct is always an option:
def switch_day(day):
if day == 1:
return "Monday"
elif day == 2:
return "Tuesday"
elif day == 3:
return "Wednesday"
elif day == 4:
return "Thursday"
elif day == 5:
return "Friday"
else:
return "Weekend"
# Example usage
day = 3
print(switch_day(day))
Comparing with Other Languages: JavaScript
In JavaScript, a switch statement looks like this:
let day = 3;
let dayName;
switch(day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
default:
dayName = "Weekend";
}
console.log(dayName);
Python vs. Other Languages
Python doesn't have a built-in switch statement or switch case like other programming languages, its flexible and dynamic nature allows for similar functionality through dictionary mappings and functions. The dictionary mapping approach is very simple but it can be more concise and more look like python syntax but compared to the traditional switch-case construct in languages like C and JavaScript.
Conclusion
Although Python doesn't have a native switch statement, you can achieve the same result using dictionary mappings, functions, and the if-elif-else
construct. Each method has its advantages, and choosing the right one depends on your specific use case. By understanding these alternatives, you can write more efficient and readable code in Python. you can find more way to write switch case,to test the equality of a variable against several values specified in the test cases.
if you find helpful this blog post hit the like button and comment there uses in real life projects.
Happy coding!