Python Match Case Exercise for 2026 Python Interview

🔹 Beginner (1–10): Basic Matching

# 1. Match day number to weekday name
def day_name(day):
    match day:
        case 1: return "Monday"
        case 2: return "Tuesday"
        case 3: return "Wednesday"
        case 4: return "Thursday"
        case 5: return "Friday"
        case 6: return "Saturday"
        case 7: return "Sunday"
        case _: return "Invalid"

# 2. Match month number to month name
def month_name(month):
    match month:
        case 1: return "January"
        case 2: return "February"
        case 3: return "March"
        case 4: return "April"
        case 5: return "May"
        case 6: return "June"
        case 7: return "July"
        case 8: return "August"
        case 9: return "September"
        case 10: return "October"
        case 11: return "November"
        case 12: return "December"
        case _: return "Invalid"

# 3. Match 1–3 to words
def number_to_word(n):
    match n:
        case 1: return "One"
        case 2: return "Two"
        case 3: return "Three"
        case _: return "Out of range"

# 4. Vowel or consonant
def vowel_consonant(char):
    match char.lower():
        case 'a' | 'e' | 'i' | 'o' | 'u': return "Vowel"
        case _: return "Consonant"

# 5. Traffic light color
def traffic_instruction(color):
    match color.lower():
        case "red": return "Stop"
        case "yellow": return "Wait"
        case "green": return "Go"
        case _: return "Invalid color"

# 6. Grade letter to comment
def grade_comment(grade):
    match grade.upper():
        case 'A': return "Excellent"
        case 'B': return "Good"
        case 'C': return "Average"
        case 'D': return "Poor"
        case 'F': return "Fail"
        case _: return "Invalid grade"

# 7. Even, odd or zero
def number_type(n):
    match n:
        case 0: return "Zero"
        case _ if n % 2 == 0: return "Even"
        case _: return "Odd"

# 8. Arithmetic operator to name
def operator_name(op):
    match op:
        case '+': return "Addition"
        case '-': return "Subtraction"
        case '*': return "Multiplication"
        case '/': return "Division"
        case _: return "Unknown operator"

# 9. Integer to Roman numeral
def roman_numeral(n):
    match n:
        case 1: return "I"
        case 2: return "II"
        case 3: return "III"
        case 4: return "IV"
        case 5: return "V"
        case _: return "Out of range"

# 10. Animal name to sound
def animal_sound(animal):
    match animal.lower():
        case "dog": return "Bark"
        case "cat": return "Meow"
        case "cow": return "Moo"
        case "lion": return "Roar"
        case _: return "Unknown animal"

🔸 Intermediate (11–20): Logic & Actions

# 11. Tuple (x, y) location
def point_location(x, y):
    match (x, y):
        case (0, 0): return "Origin"
        case (_, 0): return "X-axis"
        case (0, _): return "Y-axis"
        case _: return "General point"

# 12. Match string command
def control_command(cmd):
    match cmd.lower():
        case "start": return "Engine started"
        case "stop": return "Engine stopped"
        case "pause": return "Paused"
        case _: return "Invalid command"

# 13. File extension to type
def file_type(extension):
    match extension:
        case ".py": return "Python file"
        case ".txt": return "Text file"
        case ".docx": return "Word document"
        case _: return "Unknown file type"

# 14. Temperature to season
def temperature_to_season(temp):
    match temp:
        case t if t <= 10: return "Winter"
        case t if 11 <= t <= 25: return "Spring"
        case t if 26 <= t <= 35: return "Summer"
        case t if t > 35: return "Hot Summer"
        case _: return "Invalid"

# 15. Score to grade
def score_grade(score):
    match score:
        case s if 90 <= s <= 100: return "A"
        case s if 80 <= s < 90: return "B"
        case s if 70 <= s < 80: return "C"
        case s if 60 <= s < 70: return "D"
        case s if 0 <= s < 60: return "F"
        case _: return "Invalid"

# 16. Product code to item name
def product_lookup(code):
    match code:
        case "P101": return "Laptop"
        case "P102": return "Smartphone"
        case "P103": return "Tablet"
        case _: return "Unknown product"

# 17. Bank transaction
def bank_action(action, amount):
    match action.lower():
        case "deposit": return f"Deposited ₹{amount}"
        case "withdraw": return f"Withdrawn ₹{amount}"
        case _: return "Invalid transaction"

# 18. Currency code to name
def currency_name(code):
    match code.upper():
        case "USD": return "US Dollar"
        case "INR": return "Indian Rupee"
        case "EUR": return "Euro"
        case _: return "Unknown currency"

# 19. NATO phonetic alphabet
def nato_alphabet(letter):
    match letter.upper():
        case "A": return "Alpha"
        case "B": return "Bravo"
        case "C": return "Charlie"
        case _: return "Not mapped"

# 20. Age to life stage
def life_stage(age):
    match age:
        case a if 0 <= a <= 12: return "Child"
        case a if 13 <= a <= 19: return "Teen"
        case a if 20 <= a <= 60: return "Adult"
        case a if age > 60: return "Senior"
        case _: return "Invalid age"

🔺 Advanced (21–30): Pattern Matching & Nesting

# 21. Match list length & content
def match_list(lst):
    match lst:
        case []: return "Empty list"
        case [x]: return f"Single element: {x}"
        case [x, y]: return f"Two elements: {x}, {y}"
        case [x, *rest]: return f"First: {x}, Rest: {rest}"

# 22. Match dict keys
def classify_data(d):
    match d:
        case {"name": name, "age": age}: return f"User: {name}, Age: {age}"
        case {"product": p, "price": pr}: return f"Product: {p}, Price: ₹{pr}"
        case _: return "Unknown data"

# 23. Validate string pattern
def validate_input(text):
    match text:
        case t if "@" in t and "." in t: return "Email"
        case t if t.isdigit() and len(t) == 10: return "Phone number"
        case _: return "Invalid input"

# 24. Nested tuple (id, (name, score))
def nested_tuple(t):
    match t:
        case (id, (name, score)): return f"ID: {id}, Name: {name}, Score: {score}"
        case _: return "Invalid format"

# 25. List of scores - pass/fail
def evaluate_scores(scores):
    return [("Pass" if s >= 40 else "Fail") for s in scores]

# 26. RGB tuple
def rgb_color(color):
    match color:
        case (255, 0, 0): return "Red"
        case (0, 255, 0): return "Green"
        case (0, 0, 255): return "Blue"
        case _: return "Unknown color"

# 27. JSON-like dict extract
def extract_json(data):
    match data:
        case {"user": {"name": name, "email": email}}:
            return f"Name: {name}, Email: {email}"
        case _: return "Invalid format"

# 28. Match user role
def user_role(role):
    match role:
        case "admin": return "Redirect to admin dashboard"
        case "user": return "Redirect to user home"
        case "moderator": return "Redirect to moderation panel"
        case _: return "Unknown role"

# 29. HTTP status code
def http_status(code):
    match code:
        case 200: return "OK"
        case 404: return "Not Found"
        case 500: return "Internal Server Error"
        case _: return "Unknown status"

# 30. Complex packet structure
def data_packet(packet):
    match packet:
        case ("text", payload): return f"Text message: {payload}"
        case ("image", payload): return f"Image data of size {len(payload)} bytes"
        case ("command", cmd): return f"Execute: {cmd}"
        case _: return "Invalid packet"


0 Comments