Match statement or expression in python is the same as switch statement in Java or C++
When we want to print a certain value based on multiple conditions we can use python match statement. It is the alternative of using multiple python elif (else if) statements.
Syntax:
match expression: # also valid: match (expression):
case 1:
case_block
case 2:
case_block
case 3:
case_block
case _: # little space required
wildcard_block # always executed if no case block matches
Note: The last case_: block is a wildcard statement and never fails to match. Means, if no case block matches with the expression then it will definitely be executed.
It surprised me too, there is no break statement or default statement like Java or C++. Thus, it is considered more like pattern matching like Rust or Haskell language.
# Combines more than one or several case statements
We can combine several case literals or statements using single pattern " | "
match (expression):
case 6 | 7:
case_block
# Print the name of the week day using match statement
day = 4
match (day): # or match day:
case 1:
print("Saturday")
case 2:
print("Sunday")
case 3:
print("Monday")
case 4:
print("Tuesday")
case 5:
print("Wednesday")
case 6:
print("Thursday")
case 7:
print("Friday")
case _: # it will be executed if no match found
print("No match")
When you run the above example, you will get the following output in the console:
Thursday
# match statement in python function
def money_available(money):
match money:
case 700 | 900:
return "You can buy a Dell Laptop "
case 1000 | 1300:
return "You can buy PHP Laptop"
case 1500 | 600 | 1700:
return "You can buy a MacBook Laptop"
case 100:
return "Everything is expensive nowadays"
case _:
return "Probably you forgot to bring your wallet"
# call the function with appropriate argument
print(money_available(700))
If you run the above example and call the money_availalable() function with the appropriate argument, you will get the following output. In my case, I passed 700
The output was:
You can buy a Dell Laptop