Python Match Case

The match statement is used to perform different actions based on different conditions. It was introduced in Python 3.10 and works similarly to the switch statement in other programming languages. Syntax Simple Match Example Example: Output: Default Case Use _ as the default case when no match is found. Example: Output: Matching Strings Example: Output: […]

2 mins read Lesson 22 of 40 55% through track

The match statement is used to perform different actions based on different conditions.

It was introduced in Python 3.10 and works similarly to the switch statement in other programming languages.

Syntax

match expression:
    case value1:
        # code block
    case value2:
        # code block
    case _:
        # default code block

Simple Match Example

Example:

day = 3

match day:
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case 3:
        print("Wednesday")

Output:

Wednesday

Default Case

Use _ as the default case when no match is found.

Example:

day = 8

match day:
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case _:
        print("Invalid Day")

Output:

Invalid Day

Matching Strings

Example:

fruit = "Apple"

match fruit:
    case "Apple":
        print("Red Fruit")
    case "Banana":
        print("Yellow Fruit")
    case _:
        print("Unknown Fruit")

Output:

Red Fruit

Multiple Values in One Case

Use the | operator to match multiple values.

Example:

day = "Saturday"

match day:
    case "Saturday" | "Sunday":
        print("Weekend")
    case _:
        print("Weekday")

Output:

Weekend

Match with Variables

Example:

status = "success"

match status:
    case "success":
        print("Operation Successful")
    case "error":
        print("Operation Failed")
    case _:
        print("Unknown Status")

Output:

Operation Successful

Match with Conditions

Use if inside a case to add conditions.

Example:

age = 20

match age:
    case age if age >= 18:
        print("Adult")
    case _:
        print("Minor")

Output:

Adult

Practical Example

Simple calculator operation:

operation = "+"

match operation:
    case "+":
        print(10 + 5)
    case "-":
        print(10 - 5)
    case "*":
        print(10 * 5)
    case "/":
        print(10 / 5)
    case _:
        print("Invalid Operation")

Output:

15

Match vs If Else

Using if elif else:

day = 1

if day == 1:
    print("Monday")
elif day == 2:
    print("Tuesday")
else:
    print("Invalid Day")

Using match case:

day = 1

match day:
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case _:
        print("Invalid Day")

Both produce the same result, but match case is often cleaner when checking many values.

Important Notes

  • Available from Python 3.10 and later.
  • The _ case acts as the default case.
  • Cases are checked from top to bottom.
  • Only the first matching case is executed.

Summary

  • match is used to compare a value against multiple cases.
  • It works similarly to a switch statement.
  • Use case to define possible matches.
  • Use _ for the default case.
  • Multiple values can be matched using |.
  • match case can make code more readable than long if elif else chains.