As programs become larger and more complex, they often need to make decisions based on many different values. In previous lessons, you learned how to use the if, if...else, if...elif...else, and nested if statements to control the flow of a program. These conditional statements are powerful and are suitable for most situations.
However, when a program needs to compare one value against many possible choices, writing multiple elif statements can make the code long and difficult to read.
For example, consider the following situations:
start, stop, or exit.Writing long chains of if...elif...else statements for these situations works, but it is not always the most readable solution.
To solve this problem, Python 3.10 introduced the Match Case Statement. It provides a cleaner and more organized way to compare a single value against multiple possible cases.
The match...case statement is similar to the switch statement found in programming languages such as C, C++, Java, and JavaScript, but Python’s implementation is more powerful because it supports structural pattern matching.
After completing this lesson, you will be able to:
match...case statement.match...case instead of if...elif...else.case blocks.match statement.case _) for default conditions.match...case.The match...case statement is a control flow statement that compares a value against multiple possible cases. When Python finds the first matching case, it executes the corresponding block of code and skips the remaining cases.
Unlike the if...elif...else statement, which evaluates Boolean expressions, the match...case statement compares a single value with different patterns or constants.
This makes the code shorter, easier to read, and easier to maintain when many possible choices exist.
The match...case statement was introduced in Python 3.10.
If you are using Python 3.9 or an earlier version, the match keyword is not available, and attempting to use it will produce a syntax error.
python --version
or
python3 --version
Example Output
Python 3.12.2
If your version is 3.10 or later, you can use the match...case statement.
match variable:
case value1:
statement
case value2:
statement
case _:
statement
The syntax consists of the following parts:
match specifies the value to compare.case represents one possible value.case _ acts as the default case if no other case matches.Notice that every case block is properly indented, just like an if statement.
The execution of a match...case statement follows these steps:
match keyword.case.case.case _ block executes (if present).Only one matching case executes during a single program run.
Let’s create a simple program that displays the day of the week.
day = 2
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case _:
print("Invalid Day")
Output
Tuesday
Python compares the value of day with each case until it finds a match.
fruit = "Apple"
match fruit:
case "Apple":
print("Red Fruit")
case "Banana":
print("Yellow Fruit")
case _:
print("Unknown Fruit")
Output
Red Fruit
if...elif...else and Match Caseif...elif...else |
match...case |
|---|---|
| Works with Boolean expressions. | Matches a value against multiple cases. |
| Suitable for complex conditions. | Suitable for comparing one value with many choices. |
| Can become lengthy with many conditions. | Usually shorter and easier to read. |
| Available in all Python versions. | Available only in Python 3.10 and later. |
if...elif...elseday = 2
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
else:
print("Invalid Day")
day = 2
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case _:
print("Invalid Day")
Both programs produce the same output, but the match...case version is often easier to read when there are many possible values.
The match...case statement is useful when:
if...elif...else chains.However, if your program depends on complex Boolean expressions or mathematical comparisons, the if...elif...else statement is usually the better choice.
In this section, you learned what the Python match...case statement is, why it was introduced, and how it simplifies decision-making when comparing one value against multiple choices. You explored its syntax, execution flow, Python version requirement, and compared it with the if...elif...else statement. You also created your first match...case programs using numbers and strings. In the next section, you will learn how to work with multiple cases, wildcard patterns, grouped cases, user input, strings, numbers, and common mistakes while using the match...case statement.
Now that you understand the basic syntax of the match...case statement, it’s time to explore its features in more detail. The match...case statement can handle multiple cases, group similar cases together, work with numbers and strings, accept user input, and provide a default action using the wildcard case.
These features make it an excellent choice for building menu-driven applications and programs that need to compare a single value against many possible options.
A match statement can contain any number of case blocks. Python evaluates the cases one by one until it finds the first matching value.
number = 3
match number:
case 1:
print("One")
case 2:
print("Two")
case 3:
print("Three")
case 4:
print("Four")
case _:
print("Invalid Number")
Output
Three
Python compares the value 3 with each case until it finds a match.
case _)The wildcard case, written as case _, works like the else block in an if...elif...else statement.
It executes only when none of the previous cases match.
day = 8
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case _:
print("Invalid Day")
Output
Invalid Day
Although the wildcard case is optional, it is considered a good practice because it handles unexpected values.
Sometimes different values should produce the same result. Instead of writing separate blocks, you can group multiple values using the pipe (|) operator.
day = 6
match day:
case 1 | 2 | 3 | 4 | 5:
print("Weekday")
case 6 | 7:
print("Weekend")
case _:
print("Invalid Day")
Output
Weekend
Grouping related values reduces code duplication and improves readability.
The match...case statement works with string values as well as numbers.
fruit = "Mango"
match fruit:
case "Apple":
print("Red Fruit")
case "Banana":
print("Yellow Fruit")
case "Mango":
print("Tropical Fruit")
case _:
print("Unknown Fruit")
Output
Tropical Fruit
String matching is case-sensitive.
fruit = "apple"
match fruit:
case "Apple":
print("Match Found")
case _:
print("No Match")
Output
No Match
The values "Apple" and "apple" are different because Python distinguishes between uppercase and lowercase letters.
Numeric values are frequently used with match...case statements.
choice = 2
match choice:
case 1:
print("Addition")
case 2:
print("Subtraction")
case 3:
print("Multiplication")
case 4:
print("Division")
case _:
print("Invalid Choice")
Output
Subtraction
This style is commonly used in calculator and menu-driven programs.
The match...case statement becomes more useful when users provide the value during program execution.
choice = int(input("Enter a number (1-3): "))
match choice:
case 1:
print("Create Account")
case 2:
print("Login")
case 3:
print("Exit")
case _:
print("Invalid Option")
Sample Output
Enter a number (1-3): 2
Login
Always convert the input to the appropriate data type before using it in a match statement.
Consider the following program.
month = 5
match month:
case 1:
print("January")
case 2:
print("February")
case 5:
print("May")
case _:
print("Invalid Month")
print("Program Completed")
Execution Steps
month.case 1.case 2.case 5.match statement.Output
May
Program Completed
The match...case statement works only in Python 3.10 and later.
Without case _, unexpected values may not be handled.
Every case block must be properly indented.
Incorrect
choice = input("Enter choice: ")
match choice:
case 1:
print("Option 1")
This program compares a string with an integer, so no match occurs.
Correct
choice = int(input("Enter choice: "))
match choice:
case 1:
print("Option 1")
Like the if...elif...else statement, the match...case statement executes only the first matching case.
In this section, you learned how to use multiple case blocks, the wildcard case (case _), grouped cases, string matching, number matching, and user input with the Python match...case statement. You also followed the execution flow step by step and reviewed common beginner mistakes such as incorrect data types, missing wildcard cases, improper indentation, and using unsupported Python versions. In the next section, you will apply these concepts by building practical applications such as calculator menus, ATM menus, month selectors, day-of-the-week programs, and simple command processors.
The Python match...case statement is especially useful when a program must choose between many predefined options. It is commonly used in menu-driven applications, command processors, calculators, ATM systems, and many other programs where a single value determines the next action.
In this section, you will build several practical examples to understand how the match...case statement simplifies decision making and makes code easier to read compared to long if...elif...else statements.
This program displays the selected mathematical operation based on the user’s choice.
choice = 3
match choice:
case 1:
print("Addition")
case 2:
print("Subtraction")
case 3:
print("Multiplication")
case 4:
print("Division")
case _:
print("Invalid Choice")
Output
Multiplication
This approach is much cleaner than writing several elif statements.
An ATM allows customers to perform different banking operations.
option = 2
match option:
case 1:
print("Check Balance")
case 2:
print("Withdraw Money")
case 3:
print("Deposit Money")
case 4:
print("Mini Statement")
case _:
print("Invalid Option")
Output
Withdraw Money
day = 5
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
case _:
print("Invalid Day")
Output
Friday
month = 12
match month:
case 1:
print("January")
case 2:
print("February")
case 3:
print("March")
case 4:
print("April")
case 5:
print("May")
case 6:
print("June")
case 7:
print("July")
case 8:
print("August")
case 9:
print("September")
case 10:
print("October")
case 11:
print("November")
case 12:
print("December")
case _:
print("Invalid Month")
Output
December
The following program displays a message according to the student’s grade.
grade = "B"
match grade:
case "A":
print("Excellent")
case "B":
print("Very Good")
case "C":
print("Good")
case "D":
print("Needs Improvement")
case _:
print("Invalid Grade")
Output
Very Good
Many command-line applications execute different actions based on the command entered by the user.
command = "start"
match command:
case "start":
print("Application Started")
case "stop":
print("Application Stopped")
case "restart":
print("Application Restarted")
case "exit":
print("Application Closed")
case _:
print("Unknown Command")
Output
Application Started
Most menu-driven programs allow users to choose an option during program execution.
choice = int(input("Select an option (1-4): "))
match choice:
case 1:
print("New File Created")
case 2:
print("File Opened")
case 3:
print("File Saved")
case 4:
print("Program Closed")
case _:
print("Invalid Selection")
Sample Output
Select an option (1-4): 3
File Saved
The user’s input determines which case is executed.
Consider the following program.
color = "Green"
match color:
case "Red":
print("Stop")
case "Yellow":
print("Ready")
case "Green":
print("Go")
case _:
print("Invalid Signal")
print("Traffic Signal Checked")
Execution Steps
color.case "Red".case "Yellow".case "Green".match statement.Output
Go
Traffic Signal Checked
The match...case statement is best suited for comparing one variable against many fixed values.
Use case _ to handle unexpected input values.
Each case block should perform one clear task.
menu_choice = 2
This is more descriptive than using a variable such as x.
If your program requires mathematical comparisons such as marks >= 80, use an if...elif...else statement instead.
match...case with Python versions earlier than 3.10.case _).match...case for complex comparison expressions instead of if...elif...else.case blocks.choice = input("Enter Choice: ")
match choice:
case 1:
print("Option 1")
This program compares a string with an integer, so the case will never match.
Correct Example
choice = int(input("Enter Choice: "))
match choice:
case 1:
print("Option 1")
In this section, you applied the Python match...case statement to practical programming problems such as calculator menus, ATM systems, day and month selectors, grade processors, and command-based applications. You also learned how to use user input, followed the execution flow step by step, and explored best practices for writing clean and readable match...case programs. Finally, you reviewed common beginner mistakes and learned when the match...case statement is the right choice and when an if...elif...else statement is more appropriate. In the final section, you will review everything covered in this lesson through a summary, key takeaways, FAQs, coding exercises, interview questions, a mini project, and a preview of the next lesson on the Python for Loop.
In this lesson, you learned how the Python match...case statement provides a clean and organized way to compare a single value against multiple possible choices. Introduced in Python 3.10, this statement makes menu-driven programs and value-based decision making easier to read and maintain than long chains of if...elif...else statements.
You explored the syntax of the match...case statement, understood how Python evaluates each case, and learned the purpose of the wildcard case (case _). You also worked with numbers, strings, grouped cases, and user input while building practical applications.
Through examples such as calculator menus, ATM systems, traffic signal programs, month selectors, command processors, and grade-based programs, you learned when the match...case statement is the most suitable choice. You also discovered that it is best used for matching fixed values, while complex comparison expressions are better handled by the if...elif...else statement.
match...case statement was introduced in Python 3.10.case.case _) works like the else block.|) operator.match...case statement works with numbers, strings, and many other patterns.if...elif...else is usually a better choice.match...case statement?The match...case statement compares one value with multiple possible cases and executes the first matching case.
match...case statement?The match...case statement is available in Python 3.10 and later.
case _?case _ is the default case. It executes when none of the previous cases match.
case blocks execute during one program run?No. Only the first matching case executes.
Yes. You can group values using the pipe (|) operator.
match...case instead of if...elif...else?No. Use match...case when comparing one value with many fixed choices. Use if...elif...else for comparison expressions such as >, <, or == involving different conditions.
match...case work with user input?Yes. User input can be matched after converting it to the appropriate data type when necessary.
case blocks?Yes. Like all Python code blocks, every case block must be properly indented.
match...case statement.start, stop, and restart.|) operator.match...case statement?match...case statement?match...case different from if...elif...else?case _?case?case execute in the same program run?match...case instead of if...elif...else?match...case statement.Create a Python program that simulates a simple restaurant menu using the match...case statement.
Your program should:
case _ to display “Invalid Menu Option” for incorrect input.Restaurant Menu
1. Pizza
2. Burger
3. Pasta
4. Sandwich
5. Coffee
Enter Your Choice: 2
You selected: Burger
Thank You for Visiting Our Restaurant.
Restaurant Menu
1. Pizza
2. Burger
3. Pasta
4. Sandwich
5. Coffee
Enter Your Choice: 8
Invalid Menu Option
Thank You for Visiting Our Restaurant.
Congratulations! You have completed the Python conditional statements section. You now understand how to make decisions using the if, if...else, if...elif...else, nested if, and match...case statements. These concepts form the foundation of decision-making in Python and are essential for writing interactive programs.
In the next lesson, you will begin learning Python for Loops. You will discover how loops allow you to execute the same block of code multiple times, iterate through sequences such as strings and lists, use the range() function, control loop execution, and solve repetitive programming tasks efficiently.