Python Concepts/Match-case Statement

Objective

edit
  • Learn about Match-case statement
  • Learn about case _ (underline)
  • Learn about what's the difference between if statement and Match-case statement

Pseudocodes for Match-case Statement

edit

There are examples of how a Match-case would be in a pseudocode below.


 number_of_apples is how many I ate today
 
 match number_of_apples:
   case 0:
     I didn't eat any apple today
   case 1:
     A single apple
   case 2:
     double apple
   case _:
     Many apples


The match statement is initialized with the match keyword creating a block like if-statement and taking a parameter. In the block there are multiple cases using the case keyword and the object that can match with the parameter. If nothing is matched with the parameter case ”_” (underline) is executed.

 # where_I_am_in_my_home is a tuple or list in Python, which the 2 values are the x and y coordinates
 
 where_I_am_in_my_home = position 1 and 2
 
 match where_I_am_in_my_room:
   case 0 and 0:
     I'm at the door
   case 0 and 1:
     I'm next to TV
   case 0 and -1:
     I'm next to the sofa
   case 0 and -2:
     I'm next to my dog's bed
   case 1 and 2:
     I'm next to my family's old picture
   ...
   case 20 and -12:
     I'm next to the second toilet of my home

You can use tuples and lists as parameters, the cases need to have 2 values as the parameter would have.

 # Number from 0 to 9 that I like or don't
 
 number = the user choise
 
 match number:
   case 0 or 5 or 6:
     I don't like this number
   case 2 or 3:
     This number is one of my favorites
   case 4 or 5 or 7:
     I like this number
   case 8 or 9:
     My mom likes this number

You can unify cases to do the same action.

Codes for Match-case statement

edit

The same examples as the pseudocodes but in Python below.


 # How many apples I ate today
 number_of_apples = 2
 
 match number_of_apples:
   case 0:
     print("I didn't eat any apple today.")
   case 1:
     print("I ate a single apple today.")
   case 2:
     print("I ate a pair of apples today.")
   case _:
     print("I ate many apples today.")
 [Console]
 
 I ate a pair of apples today.
 
 [Finished program]

The match statement is initialized with the match keyword creating a block like if-statement and taking a parameter. In the block there are multiple cases using the case keyword and the object that can match with the parameter. If nothing is matched with the parameter case ”_” (underline) is executed.

In this example, I ate 2 apples today. The integer 2 is saved in memory with the name 'number_of_apples', which is variable. In these cases, which one matches with the object 'number_of_apples' where is saved the integer value 2? It's case 2. I ate a pair of apples today.

 # where_I_am_in_my_home is a list, which the 2 values are the x and y coordinates, it can be a tuple (immutable) too
 
 where_I_am_in_my_home = [1, 2]
 
 match where_I_am_in_my_home:
   case [0, 0]:
     print("I'm at the door.")
   case [0, 1]:
     print("I'm next to TV.")
   case [0, -1]:
     print("I'm next to the sofa.")
   case [0, -2]:
     print("I'm next to my dog's bed.")
   case [1, 2]:
     print("I'm next to my family's old picture.")
   ...
   case [20, -12]:
     print("I'm next to the second toilet of my home.")
 [Console]
 
 I'm next to my family's old picture.
 
 [Finished program]

My coordinates are [1, 2]. In this case, I'm next to my family's old picture.

 # Number from 0 to 9 that I like or don't
 number = int(input("User, choose a number from 0 to 9: "))

The input should be a integer, if don't: a error will happen. You will learn how to deal with this case later

 [Console]
 
 User, choose a number from 0 to 9: some string
 ValueError: invalid literal for int() with base 10
 
 [Program Finished]

The code...

 # Number from 0 to 9 that I like or don't
 number = int(input("User, choose a number from 0 to 9: "))
 
 match number:
   case 0 | 5 | 6:
     print("I don't like this number.")
   case 2 | 3:
     print("This number is one of my favorites.")
   case 4 | 5 | 7:
     print("I like this number.")
   case 8 | 9:
     print("My mom likes this number.")

Let's suppose the user chose the number 7, the program will print in the console...

 [Console]
 
 I like this number.
 
 [Program finished]

The program will print this because the variable 'number' has the value 7 which matches with the case 4 or 5 or 7, the action is to print "I like this number." string in console.

About the problem in int(input("User, choose a number from 0 to 9: ")), you may don't understand something. The solution is...

 # Number from 0 to 9 that I like or don't
 while 1:
   try:
     number = int(input("User, choose a number from 0 to 9: "))
     if number >= 0 and number <= 9:
       break
     else:
       print("This number is not between 0 and 9. Try again.")
   except:
      print("Invalid input. Try again.")
 
 match number:
   case 0 | 5 | 6:
     print("I don't like this number.")
   case 2 | 3:
     print("This number is one of my favorites.")
   case 4 | 5 | 7:
     print("I like this number.")
   case 8 | 9:
     print("My mom likes this number.")

Comparison between If statement and Match-case statement

edit

The first example rewrote with if statement

 # How many apples I ate today
 number_of_apples = 2
 
 if number_of_apples == 0:
   print("I didn't eat any apple today.")
 elif number_of_apples == 1:
   print("I ate a single apple today.")
 elif number_of_apples == 2:
   print("I ate a pair of apples today.")
 else:
   print("I ate many apples today.")
 [Console]
 
 I ate a pair of apples today.
 
 [Finished program]

The second example rewrote with if statement

 # where_I_am_in_my_home is a list, which the 2 values are the x and y coordinates
 where_I_am_in_my_home = [1, 2]
 
 if where_I_am_in_my_home[0] == 0 and where_I_am_in_my_home[1] == 0:
     print("I'm at the door.")
 elif where_I_am_in_my_home[0] == 0 and where_I_am_in_my_home[1] == 1:
     print("I'm next to TV.")
 elif where_I_am_in_my_home[0] == 0 and where_I_am_in_my_home[1] == -1:
     print("I'm next to the sofa.")
 elif where_I_am_in_my_home[0] == 0 and where_I_am_in_my_home[1] == -2:
     print("I'm next to my dog's bed.")
 elif where_I_am_in_my_home[0] == 1 and where_I_am_in_my_home[1] == 2:
     print("I'm next to my family's old picture.")
   ...
 elif where_I_am_in_my_home[0] == 20 and where_I_am_in_my_home[1] == -12:
     print("I'm next to the second toilet of my home.")
 [Console]
 
 I'm next to my family's old picture.
 
 [Finished program]

The third example rewrote with if statement

 # Number from 0 to 9 that I like or don't
 number = int(input("User, choose a number from 0 to 9: "))
 
 if number == 0 or number == 5 or number == 6:
   print("I don't like this number.")
 elif number == 2 or number == 3:
   print("This number is one of my favorites.")
 elif number == 4 or number == 5 or number == 7:
   print("I like this number.")
 elif number == 8 or number == 9:
   print("My mom likes this number.")

Let's suppose the user chose the number 7, the program will print in the console...

 [Console]
 
 I like this number.
 
 [Program finished]

The code building is different, but the objective is the same.

Page index

edit

References

edit

1. Python 3.12.4's documentation:

Control Flow: 4.6 Match statements