Lesson 05
match..case Statement — Menu-Driven Programs (Python 3.10+)
Use Python match-case for menu-driven apps: arithmetic operations, base conversion, weekday classification, and default case handling.
Beginner35 min
match..case statement (Python 3.10 onwards)
match..case is a conditional statement for pre-designed conditions and menu-driven applications.
Syntax
match choice_expr:
case choice_label_1:
block_of_statements_1
case choice_label_2:
block_of_statements_2
case choice_label_n:
block_of_statements_n
case _:
default_block_of_statements
# Other statements in programExplanation
matchandcaseare keywords.choice_expris typicallyint,str, orbool.- If
choice_exprmatches a case label, PVM executes that block, then continues with other statements in the program. - If no case matches, PVM executes the default block (
case _). - Writing the default case is optional; if used, it must be last (otherwise
SyntaxError). - Multiple case labels under one
caseare combined with the bitwise OR operator|.
Example 1 — Arithmetic operations menu
print("=" * 50)
print("\tARITHMETIC OPERATIONS")
print("=" * 50)
print("\t1.Addition")
print("\t2.Substraction")
print("\t3.Multiplication")
print("\t4.Division")
print("\t5.Modulo Div")
print("\t6.Exponentiation")
print("\t7.Exit")
ch = int(input("Enter Ur Choice:"))
match ch:
case 1:
a, b = float(input()), float(input())
print("Sum({},{})={}".format(a, b, a + b))
case 2:
a, b = float(input()), float(input())
print("Sub({},{})={}".format(a, b, a - b))
case 7:
print("Thx for using program")
case _:
print("Ur selection of Operation is wrong--try again")
print("Program execution completed")Example 2 — Weekday classification with pipe patterns
wkd = input("Enter Week Name:").upper()
match wkd:
case "MONDAY" | "TUESDAY" | "WEDNESDAY" | "THURSDAY" | "FRIDAY":
print("{} is Working Day".format(wkd))
case "SATURDAY":
print("{} is Weekd END".format(wkd))
case "SUNDAY":
print("{} is Holi Day".format(wkd))
case _:
print("{} is not a week day--try again".format(wkd))Example 3 — Base conversion calculator
Grouped cases with | handle 12 conversion options from the teaching notes:
s = """
=======================================================
Base Conversion Calculator
=======================================================
1. D to B
2. D to O
3. D to H
------------
4. B to D
5. B to O
6. B to H
-------------
7. O to D
8. O to B
9. O to H
------------
10. H to D
11. H to B
12. H to O
====================================================="""
print(s)
ch = int(input("Enter Ur Choice:"))
match ch:
case 1 | 2 | 3:
d = int(input("Enter Decimal Number:"))
bv = bin(d)
ov = oct(d)
hv = hex(d)
print("Bin({})={}".format(d, bv))
print("Oct({})={}".format(d, ov))
print("Hex({})={}".format(d, hv))
case 4 | 5 | 6:
x = input("enter binary value preceded with 0b:")
dv = int(x, 2)
ov = oct(int(x, 2))
hv = hex(int(x, 2))
print("Dec({})={}".format(x, dv))
print("Oct({})={}".format(x, ov))
print("hex({})={}".format(x, hv))
case 7 | 8 | 9:
x = input("enter Octal value preceded with 0o:")
dv = int(x, 8)
bv = bin(int(x, 8))
hv = hex(int(x, 8))
print("Dec({})={}".format(x, dv))
print("Bin({})={}".format(x, bv))
print("hex({})={}".format(x, hv))
case 10 | 11 | 12:
x = input("enter hex decimal value preceded with 0x:")
dv = int(x, 16)
bv = bin(int(x, 16))
ov = oct(int(x, 16))
print("Dec({})={}".format(x, dv))
print("Bin({})={}".format(x, bv))
print("Oct({})={}".format(x, ov))
case _:
print("Ur selection of Operation is Wrong-Try again")Example menus from notes
Area of different figures:
R. Rectangle
S. Square
C. Circle
T. Triangle
E. ExitTemperature conversion calculator (homework):
| Choice | Conversion |
|---|---|
| 1 | Fahrenheit to Celsius: C = (F − 32) × (5/9) |
| 2 | Fahrenheit to Kelvin: K = (F − 32) × (5/9) + 273.15 |
| 3 | Celsius to Fahrenheit: F = C × (9/5) + 32 |
| 4 | Celsius to Kelvin: K = C + 273.15 |
| 5 | Kelvin to Celsius: C = K − 273.15 |
| 6 | Kelvin to Fahrenheit: F = (K − 273.15) × (9/5) + 32 |
| 7 | Exit |
Knowledge check
Q1: What does case _ mean?
Answer: Default case — runs when no other case matches.
Q2: How combine multiple labels in one case?
Answer: Use | — e.g. case "MON" | "TUE":
Next: File 06 — while Loop
Loading playground…