Switch Case in Python (Replacement)
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    15 Sep, 2025
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                Unlike many other languages (like C++ or Java), Python does not have a built-in switch or case statement. However, there are multiple ways to achieve similar functionality.
Method 1:  Using Dictionary Mapping
Dictionary mapping lets us replace switch-case by mapping keys to values or functions. It makes the code shorter, cleaner, and easier to manage than long if-elif chains.
Example:
            Python
    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)  
Explanation:
- switcher = {1: one, 2: two, 3: three} maps numbers to functions.
- When value = 2, switcher.get(value) fetches the function two.
- If the key doesn’t exist, the default lambda: "unknown" is returned.
- Adding () calls the fetched function, so two () runs.
Method 2: Using If-Elif-Else
The if-else is another method to implement switch case replacement. It is used to determine whether a specific statement or block of statements will be performed or not.
            Python
    bike = 'Yamaha'
if bike == 'Hero':
    print("bike is Hero")
elif bike == "Suzuki":
    print("bike is Suzuki")
elif bike == "Yamaha":
    print("bike is Yamaha")
else:
    print("Please choose correct answer")
Method 3:  Using Class Method
In this method, we are using a class to create a switch method inside the switch class in Python.
            Python
    class Switch:
    def case_1(self):
        return "one"
    def case_2(self):
        return "two"
    def case_3(self):
        return "three"
    def default(self):
        return "unknown"
    def switch(self, value):
        # getattr fetches method by name, default → self.default
        return getattr(self, f"case_{value}", self.default)()
obj = Switch()
print(obj.switch(2))  
Explanation:
- Switch class groups all the case methods (case_1, case_2, case_3) and a default method.
- switch() method uses getattr to find a method dynamically by its name (e.g., "case_2").
- If the method exists, it is returned; if not, self.default is used as fallback.
- Adding () calls the chosen method.
- For obj.switch(2), it looks for "case_2" → calls it → returns "two".
Method 4: Using Match-Case (Python 3.10+)
In Python 3.10 and after that, Python will support this by using match in place of switch:
            Python
    value = 2
match value:
    case 1:
        result = "one"
    case 2:
        result = "two"
    case 3:
        result = "three"
    case _:
        result = "unknown"
print(result)  # Output: two
Explanation:
- value = 2 sets the number to be checked.
- The match statement compares value against each case.
- When it finds a match (case 2), it assigns "two" to result.
- case _ acts as the default if no other case matches.
Note: It is similar to that of switch cases in C++, Java, etc.
                                
                                
                            
                                                                                
                                                            
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice