Python Switch Case Statement Example: Simple Guide

The concept of a switch case statement is not natively supported in Python, unlike in languages like C, C#, or Java. However, Python developers frequently use the Dictionary data structure and the 'match' statement (introduced in Python 3.10) to achieve similar functionality. Let's dive into an example that demonstrates how to use a 'match' statement for switch-case type behavior in Python.

Python Match Case with Code Example
Python Match Case with Code Example

Before we proceed, ensure that you have_upgraded to Python 3.10 or later, as the 'match' statement is a recent addition.

the if statement in python is true or false, and it's very easy to read
the if statement in python is true or false, and it's very easy to read

Implementing Switch Case in Python using Dictionary and 'match'

The 'match' statement in Python works similarly to switch-case statements in other programming languages. It allows us to test an expression against one or more patterns and execute code blocks corresponding to the first matching pattern.

Python match-case (switch-case?)
Python match-case (switch-case?)

We'll create a function that takes a day of the week as input and returns its corresponding weekday number, just like a switch-case statement would do.

Basic Syntax and Example

Python If...Elif...Else Statement - PythonForBeginners.com
Python If...Elif...Else Statement - PythonForBeginners.com

The basic syntax of the 'match' statement is as follows:

variable match case pattern:

# code block 1

🔁 Return Statement in Python: Send Values Like a Pro!
🔁 Return Statement in Python: Send Values Like a Pro!

... case pattern:

# code block 2

... default:

the if statement in python is used to execute a block code
the if statement in python is used to execute a block code

# default code block

The 'default' case is optional and acts like the 'default' case in traditional switch-case statements.

C++ | Switch Statement
C++ | Switch Statement
an image of a computer screen with the words printf and break in different languages
an image of a computer screen with the words printf and break in different languages
Complete Python Built-in Functions with Examples
Complete Python Built-in Functions with Examples
Python if statement guide
Python if statement guide
Python if elif else Statement with Example Programs - EasyCodeBook.com
Python if elif else Statement with Example Programs - EasyCodeBook.com
the if statement in python is used to execute a block of code information for example
the if statement in python is used to execute a block of code information for example
Python Unit 3 Cheat Sheet 🐍 | Functions, Arguments, Lambda & Modules (AKTU)
Python Unit 3 Cheat Sheet 🐍 | Functions, Arguments, Lambda & Modules (AKTU)
a black screen with text that reads, 3 types of python functions in the same language
a black screen with text that reads, 3 types of python functions in the same language
Python Wallpaper, Python Code, Robotics Projects, Python Programming, Nikola Tesla, Deep Learning, Computer Technology, Python, Web Development
Python Wallpaper, Python Code, Robotics Projects, Python Programming, Nikola Tesla, Deep Learning, Computer Technology, Python, Web Development
Library vs Module vs Package in Python: Differences and Examples
Library vs Module vs Package in Python: Differences and Examples
a poster with the words python master notes
a poster with the words python master notes
an image of two lists with the text, 2 unzipping lists and 3 random lists
an image of two lists with the text, 2 unzipping lists and 3 random lists
two black and gold posters with different types of wires on them, one in the middle
two black and gold posters with different types of wires on them, one in the middle
tuple in python
tuple in python
Python Unit 2 Cheat Sheet 🐍 | If-Else, Loops, Break & Continue (AKTU)
Python Unit 2 Cheat Sheet 🐍 | If-Else, Loops, Break & Continue (AKTU)
How to Print Exception in Python with Examples
How to Print Exception in Python with Examples
Python set access cheat sheet
Python set access cheat sheet
an info sheet with text describing how to use the code for each element in this project
an info sheet with text describing how to use the code for each element in this project
a black background with text that reads, 5 combining multiple lists and 6 handling different length tables
a black background with text that reads, 5 combining multiple lists and 6 handling different length tables
Complete Python Basics Guide 🐍 | Syntax for Beginners  |
Complete Python Basics Guide 🐍 | Syntax for Beginners |

Demonstrating a Python Switch Case

Let's create a simple 'match' statement that behaves like a switch-case statement. We'll define a dictionary mapping day names to weekday numbers and use it in our 'match' statement.

def weekday_to_number(day):

weekday_dict = {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7}

match day:

case _ if day in weekday_dict:

return weekday_dict[day]

case _:

return "Invalid day provided!"

Now, let's test our function with various inputs:

The function should return weekday numbers for valid inputs (e.g., weekday_to_number('Monday') => 1) and an error message for invalid inputs (e.g., weekday_to_number('Invalid') => "Invalid day provided!").

While the 'match' statement doesn't provide complete functionality like traditional switch-case statements (e.g., it doesn't support fall-through or breaking out of a 'match' statement), it's a significant step towards having switch-case statements in Python. The language has come a long way, and with each new release, it gains more and more features that were previously only available in other languages.

Related Articles

Len W3schools Do While Loop In Python W3schools While Loop Python W3 Python List Comprehension In List Comprehension Difference Between List And Tuple In Python W3schools Nltk Python 3 Python3 Matplotlib Plot Python Math Module W3schools Linked List In Python W3schools Python Switch Case Statement Example