The match statement in Python is used to compare a value against multiple patterns and execute the block that matches.
Instead of writing many if..else statements, you can use the match statement.
Syntax :
Python
match value: case pattern1: statements case pattern2: statements case _: default statements The _ symbol acts as a default case.
Example :
Python
day = 3 match day: case 1: print("Monday") case 2: print("Tuesday") case 3: print("Wednesday") case _: print("Invalid day") #output Wednesday Python
num = 15 match num: case x if x > 0: print("Positive number") case x if x < 0: print("Negative number") case _: print("Zero") #output Positive number