Match Case Statement with Python If Condition

We also can use the Python If condition along with match case statement when instead of matching the exact value, we use a condition. Based on the condition, if the value is True and matches the case pattern, the code block is executed.

Example: In this example, we will use if condition along with match case statement to check if a number entered bt the user id positive, negative or zero.

Python
# python match case with if condition
def runMatch():
    num = int(input("Enter a number: "))
    
    # match case
    match num:
        # pattern 1
        case num if num > 0:
            print("Positive")
        # pattern 2
        case num if num < 0:
            print("Negative")
        # default pattern
        case _:
            print("Zero")
            
runMatch()

Output:

Enter a number: -15
Negative

Python Match Case Statement

Developers coming from languages like C/C++ or Java know that there is a conditional statement known as a Switch Case. This Match Case is the Switch Case of Python which was introduced in Python 3.10. Here we have to first pass a parameter and then try to check with which case the parameter is getting satisfied. If we find a match we will execute some code and if there is no match at all, a default action will take place.

Similar Reads

Python Match Case Statement Syntax

The match case statement in Python is initialized with the match keyword followed by the parameter to be matched. Then various cases are defined using the case keyword and the pattern to match the parameter. The “_” is the wildcard character that runs when all the cases fail to match the parameter value....

Simple Match Case Statement

In a simple Python match case statement, the exact value is compared and matched with the case pattern value. There are different test cases and their corresponding code which will execute only when a case is matched. Otherwise, there is a default case which executes when all the defined cases are not matched....

Match Case Statement with OR Operator

Match case statement in Python are meant to be for only matching the patterns and specific keywords or parameters. But we can also use match case statement in python when there are more than one case that results in same output. In this case, we can use pipe operator, also known as OR Operator in match case statement....

Match Case Statement with Python If Condition

We also can use the Python If condition along with match case statement when instead of matching the exact value, we use a condition. Based on the condition, if the value is True and matches the case pattern, the code block is executed....

Match Case with the Sequence Pattern

Python match case statements are commonly used to match sequence patterns such as lists and strings. It is quite easy and can use positional arguments for checking the the patterns....

Match Case Statement with Python Dictionary

Python match case statements can handle dictionary as well. It can match a single key or multiple keys. The keys and values must reside in the dictionary, if there is any misplaced value or any key which does not exist and does not match the actual dictionary and value, that case will be discarded....

Match Case Statement with Python Class

We can also use Python classes to match cases using the Python match case statements. This will make the code very much clean,  neat, and more importantly easily readable. It makes the use of the Python dataclasses module....

Python Match Case Statement FAQ

Q: What is the match case statement in Python?...