You can use guards:

match a:
    case _ if a < 42:
        print('Less')
    case _ if a == 42:
        print('The answer')
    case _ if a > 42:
        print('Greater')

Another option, without guards, using pure pattern matching:

match [a < 42, a == 42]:
    case [True, False]:
        print('Less')
    case [_, True]:
        print('The answer')
    case [False, False]:
        print('Greater')
Answer from Ajax1234 on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › is it "right" to use the match/case statement rather than if/else when you just want to check certain conditions?
r/learnpython on Reddit: Is it "right" to use the match/case statement rather than if/else when you just want to check certain conditions?
December 11, 2024 -

I can't say I fully understand the match/case statement yet, but I know a part of it involves more than just simple pattern matching. I know that the expression in the match statement, for example, actually runs and creates a result, such as an object if it's a call to a class.

So I'm wondering, if I don't want to do all that and just want to use it as a cleaner version of if/else, is this considered Pythonic, or is it overkill?

For example:

if some_value == True and some_other == 1:
    do something
elif some_value == False and some_other == 1:
    do something else
etc etc etc

or:

match (some_value, some_other):
    case (True, 1):
        do something
    case (False, 1):
        do something else
    etc etc etc

First off, I *think* I'm getting the syntax correct! Second, I'm not actually creating any values with the expressions, so it feels like a glorified if/else construct, just cleaner looking.

Is this a valid use of match/case?

Thanks!

🌐
Python.org
discuss.python.org › python help
Match/case syntax - Python Help - Discussions on Python.org
October 24, 2022 - In the dark ages when I used to code in vbScript I found this form of the select/case to be clearer than repeated if/else if blocks. I there a form of the new match/case in Python that accomplishes the same thing? I am aware that there are easier ways of coding this particular example but the beauty of this form is that the conditions in the case clauses do not have to be related. select case True case distance
🌐
Mimo
mimo.org › glossary › python › match-statement
Python Match Statement: A Versatile Switch-Case in Python
http_status = 404 match http_status: case 200: print("OK") case 404: print("Not Found") case 500: print("Server Error") case _: # The wildcard `_` acts as the default case print("Unknown status") # Outputs: Not Found · The match statement is a powerful tool for structural pattern matching, which is more advanced than a simple if/elif chain. The Python syntax starts with the match keyword, followed by an expression.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-match-case-statement
Python Match Case Statement - GeeksforGeeks
December 11, 2025 - In this example, the function check_number(x) uses a match-case statement to compare the value of x to the constants 10 and 20. If x equals 10, it prints "It's 10". If x equals 20, it prints "It's 20". If neither condition is met, the wildcard _ matches any value, leading to the message "It's neither 10 nor 20". Let's take a look at python match case statement in detail:
🌐
Plain English Westminster
benhoyt.com › writings › python-pattern-matching
Structural pattern matching in Python 3.10
A critical but informative look at the new structural pattern matching feature in Python 3.10, with real-world code examples.
🌐
Reddit
reddit.com › r/learnpython › how to - write a simple switch-case statement in python with conditional checks 3.10+
r/learnpython on Reddit: How to - write a simple switch-case statement in python with conditional checks 3.10+
October 30, 2023 -

So odd - Claude.ai is generating a simple switch-case statement in python with conditional checks 3.10+ that is not working in Google Collab laboratoryAlso when searching for more info on peps.python.org the description of guards in switch-case stmt is not easy to comprehend. The simple question I’m looking for is can I have logical conditions in switch-case stmt in Python v 3.10+. Thanks

From claude.ai

This is not correct - Python does support logical conditions in match-case statements.
For example:
python
Copy code
x = 5
match x:
case x > 0 and x > 10:
print("x is greater than 10")
case x > 0 and x < 10:
print("x is between 0 and 10")

🌐
Geek Python
geekpython.in › match-case-in-python
How To Use Match Case Statement For Pattern Matching In Python
August 15, 2023 - Here, if the user inputs a value greater than 10 then the first condition will be going to be true and the program terminates but if the value is less than 10, the program will check the first condition if that’s not true then it will move ...
Find elsewhere
🌐
Quora
quora.com › How-can-I-use-comparison-operators-with-a-match-statement-in-Python
How to use comparison operators with a match statement in Python - Quora
Answer: From the PEP that defines Python’s pattern matching: [code]match command.split(): case ["go", direction] if direction in current_room.exits: current_room = current_room.neighbor(direction) case ["go", _]: print("Sorry, you can't go that way") [/code]The first patt...
🌐
Codefinity
codefinity.com › blog › Match-case-Operators-in-Python
Match-case Operators in Python
Learn about Python's match-case operators introduced in version 3.10. They offer a more efficient way to handle conditional statements compared to if-elif-else. With patterns, you can specify rules for variable values.
🌐
The Teclado Blog
blog.teclado.com › python-match-case
Using "match...case" in Python 3.10
October 26, 2022 - In dictionary matching, the case will still match even if the input dictionary has more properties than the ones that the case specifies. If you want to dive even deeper, see here for the official documentation.
🌐
Python.org
discuss.python.org › ideas
F-strings in match case - Ideas - Discussions on Python.org
June 30, 2023 - Allow f-strings to be evaluated in a match case setting match Foo: case f"Greater than {Bar}": print("Foo > Bar") This currently raises a SyntaxError: patterns may only match literals and attribute lookups
🌐
Udacity
udacity.com › blog › 2021 › 10 › python-match-case-statement-example-alternatives.html
Python Match-Case Statement: Example & Alternatives | Udacity
September 27, 2022 - Once a matching case clause is found, the code inside that case clause is run. If there is no matching case clause for expression, the code under the default clause gets executed. Let’s look at how to implement this in Python 3.10, using the new match-case statement.
🌐
W3Schools
w3schools.com › python › python_match.asp
Python Match
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The match statement is used to perform different actions based on different conditions. Instead of writing many if..else statements, you can use the match statement. The match statement selects one of many code blocks to be executed. match expression: case x: code block case y: code block case z: code block
🌐
Python Morsels
pythonmorsels.com › match-case-parsing-python
Appreciating Python's match-case by parsing Python code - Python Morsels
June 22, 2022 - Both of those blocks of code say "I have a Call object which contains an Attribute object which has a specific attr and also contains a Name with a certain id". But the match-case statement does that so much more succinctly and I found it much more readable than the equivalent if-elif.
🌐
Python
peps.python.org › pep-0622
PEP 622 – Structural Pattern Matching | peps.python.org
Use continue and break in case clauses. ... This PEP proposes to add a pattern matching statement to Python, inspired by similar syntax found in Scala, Erlang, and other languages.
🌐
Plain English
plainenglish.io › blog › how-to-use-the-match-statement-in-python-896fbbc79d0e
How to Use the Match Statement in Python
It is impractical to write 99 values for a single case, but luckily Python has the feature that we need: we can add if statements to a case. ... num = 5 match num: case 0: print("It is zero") case n if n<100: print(n, "less than 100 but bigger than zero") case _: print("A really big number")
🌐
Medium
medium.com › @python-javascript-php-html-css › understanding-pythons-match-case-syntaxerror-when-comparing-lists-43b23f80b73d
Understanding Python’s Match-Case SyntaxError When Comparing Lists
October 31, 2024 - This syntax error generally arises because match-case isn’t optimized to handle list comparisons directly; instead, it works better when comparing strings, literals, or tuples. To get around this, each element needs to be manually specified as a case, rather than as a list.