Switch Case in Python (Replacement) - GeeksforGeeks
Switch Case in Python (Replacement) - GeeksforGeeks
Python3
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 1/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
# Driver program
17 if __name__ == "__main__":
18 argument=0
19 print (numbers_to_strings(argument))
Output
zero
Python
1 bike = 'Yamaha'
2
3 if bike == 'Hero':
4 print("bike is Hero")
5
6 elif bike == "Suzuki":
7 print("bike is Suzuki")
8
9 elif bike == "Yamaha":
10 print("bike is Yamaha")
11
12 else:
13 print("Please choose correct answer")
Output
bike is Yamaha
In this method, we are using a class to create a switch method inside the
python switch class in Python.
Python
1 class Python_Switch:
2 def day(self, month):
3
4 default = "Incorrect day"
5
6 return getattr(self, 'case_' + str(month), lambda:
default)()
7
8 def case_1(self):
9 return "Jan"
10
11 def case_2(self):
12 return "Feb"
13
14 def case_3(self):
15 return "Mar"
16
17
18 my_switch = Python_Switch()
19
20 print(my_switch.day(1))
21
22 print(my_switch.day(3))
Output
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 3/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
Jan
Mar
Python
Python does not have a built-in switch-case statement like some other
programming languages. However, you can achieve similar functionality
using alternatives such as dictionaries, if-elif-else chains, or the match-
case statement introduced in Python 3.10.
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 4/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
If-Elif-Else Chains:
value = 2
if value == 1:
result = "one"
elif value == 2:
result = "two"
elif value == 3:
result = "three"
else:
result = "unknown"
Dictionaries:
def one():
return "one"
def two():
return "two"
def three():
return "three"
switcher = {
1: one,
2: two,
3: three
}
value = 2
result = switcher.get(value, lambda: "unknown")()
print(result) # Output: two
value = 2
match value:
case 1:
result = "one"
case 2:
result = "two"
case 3:
result = "three"
case _:
result = "unknown"
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 5/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
Example:
value = 2
match value:
case 1:
result = "one"
case 2:
result = "two"
case 3:
result = "three"
case _:
result = "unknown"
Example:
def one():
return "one"
def two():
return "two"
def three():
return "three"
switcher = {
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 6/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
1: one,
2: two,
3: three
}
value = 2
result = switcher.get(value, lambda: "unknown")()
print(result) # Output: two
Explanation:
match value:
case 1:
result = "one"
case 2:
result = "two"
case _:
result = "unknown"
point = (1, 2)
match point:
case (0, 0):
result = "Origin"
case (x, 0):
result = f"X-axis at {x}"
case (0, y):
result = f"Y-axis at {y}"
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 7/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
Looking to dive into the world of programming or sharpen your Python skills?
Our Master Python: Complete Beginner to Advanced Course is your ultimate
guide to becoming proficient in Python. This course covers everything you need
to build a solid foundation from fundamental programming concepts to
advanced techniques. With hands-on projects, real-world examples, and
expert guidance, you'll gain the confidence to tackle complex coding
challenges. Whether you're starting from scratch or aiming to enhance your
skills, this course is the perfect fit. Enroll now and master Python, the language
of the future!
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 8/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
Similar Reads
Switch Case in Python (Replacement)
In this article, we will try to understand Switch Case in Python (Replacement). What is the replacement of
Switch Case in Python?Unlike every other programming language we have used before, Python does not hav…
5 min read
3 min read
3 min read
1 min read
Switch case in R
Switch case statements are a substitute for long if statements that compare a variable to several integral
values. Switch case in R is a multiway branch statement. It allows a variable to be tested for equality against…
2 min read
3 min read
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 9/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
In Python, copying and replacing files is a common task facilitated by modules like `shutil` and `os`. This
process involves copying a source file to a destination location while potentially replacing any existing file…
2 min read
Python String replace() Method
The replace() method replaces all occurrences of a specified substring in a string and returns a new string
without modifying the original string. Let’s look at a simple example of replace() method. [GFGTABS] Python…
2 min read
10 min read
2 min read
2 min read
6 min read
2 min read
5 min read
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 10/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
Sometimes we need to convert string values in a pandas dataframe to a unique integer so that the algorithms
can perform better. So we assign unique numeric value to a string value in Pandas DataFrame. Note: Before…
3 min read
Replacing column value of a CSV file in Python
Let us see how we can replace the column value of a CSV file in Python. CSV file is nothing but a comma-
delimited file. Method 1: Using Native Python way Using replace() method, we can replace easily a text into…
2 min read
3 min read
5 min read
1 min read
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 11/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
Company Languages
About Us Python
Legal Java
In Media C++
Contact Us PHP
Advertise with us GoLang
GFG Corporate Solution SQL
Placement Training Program R Language
GeeksforGeeks Community Android Tutorial
Tutorials Archive
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 12/13
12/17/24, 1:52 PM Switch Case in Python (Replacement) - GeeksforGeeks
https://www.geeksforgeeks.org/switch-case-in-python-replacement/ 13/13