Python match-case Statement: Modern Conditional Logic শেখার সহজ গাইড

 

Introduction

Python 3.10 থেকে একটি নতুন feature যোগ হয়েছে যার নাম match-case statement`। এটি অনেকটা অন্য programming language-এর switch-case এর মতো কাজ করে, কিন্তু Python এটাকে আরও powerful করেছে structural pattern matching ব্যবহার করে।

আগে Python-এ অনেক condition handle করতে হলে সাধারণত ব্যবহার করা হতো:

  • if

  • elif

  • else

কিন্তু অনেক condition থাকলে code বড় হয়ে যেত এবং readability কমে যেত। match-case ব্যবহার করলে code short, clean এবং readable হয়।

এই tutorial-এ আমরা শিখবো:

  • match-case কী

  • basic syntax

  • if-elif vs match-case

  • real programming examples

  • data science ও web development use cases


Traditional Method: if-elif-else

Python 3.10 এর আগে developers multiple conditions handle করতো if-elif-else দিয়ে।

Example:

day = "Sunday"

if day == "Saturday" or day == "Sunday":
print("Weekend")
elif day in ["Monday","Tuesday","Wednesday","Thursday","Friday"]:
print("Weekday")
else:
print("Invalid day")

Output:

Weekend

এখানে condition বেশি হলে code verbose এবং complex হয়ে যায়।


Python match-case কী?

match-case হলো Python-এর pattern matching system যা data structure-এর pattern match করে।

এটি Python-এ এসেছে Python 3.10 থেকে।

এটি ব্যবহার করলে:

✔ code concise হয়
✔ readability বাড়ে
✔ complex structure match করা যায়


Basic Syntax

match variable:
case pattern1:
code
case pattern2:
code
case _:
default_code

এখানে _ হলো default case


Example: Weekend বা Weekday Check

day = "Monday"

match day:
case "Saturday" | "Sunday":
print("Weekend")
case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
print("Weekday")
case _:
print("Invalid day")

Output

Weekday

Important Concepts

  • | → OR operator

  • _ → default case

  • match হলে automatically break হয়


if-elif vs match-case Comparison

Featureif-elifmatch-case
Python Versionসব versionPython 3.10+
Syntaxverboseconcise
Complex patternনাহ্যাঁ
Default caseelse_
Readabilityকম হতে পারেবেশি

Example: Animal Classification

Data processing এ অনেক সময় category detect করতে হয়।

animal = "Tiger"

match animal:
case "Lion" | "Tiger":
print("Mammal")
case "Eagle" | "Parrot":
print("Bird")
case "Snake" | "Crocodile":
print("Reptile")
case _:
print("Unknown")

Output

Mammal

Example: Web Application Logic

Web development-এ HTTP method handle করার জন্য match-case ব্যবহার করা যায়।

method = "GET"

match method:
case "GET":
print("Fetch data")
case "POST":
print("Create data")
case "PUT":
print("Update data")
case "DELETE":
print("Delete data")
case _:
print("Unsupported method")

Output

Fetch data

Example: API Status Code Handling

API response handle করার সময় এটি খুব useful।

status = 404

match status:
case 200:
print("Success")
case 404:
print("Page not found")
case 500:
print("Server error")
case _:
print("Unknown error")

Output

Page not found

Complex Pattern Matching Example

match-case dictionary বা complex data structure match করতে পারে।

config = {
"type": "database",
"name": "MySQL",
"version": 8
}

match config:
case {"type": "database", "name": name, "version": version}:
print(f"Database: {name}, Version: {version}")
case {"type": "cache", "name": name}:
print(f"Cache system: {name}")
case _:
print("Unknown configuration")

Output

Database: MySQL, Version: 8

এখানে dictionary থেকে values extract করা হয়েছে automatically


Common Mistakes

1️⃣ Default case না দেওয়া

case _

না দিলে unexpected behaviour হতে পারে।


2️⃣ Python version সমস্যা

match-case শুধু Python 3.10+ এ কাজ করে।


Real-life Analogy

ধরো Kolkata-র একটি food delivery app বানাচ্ছো।

User order type অনুযায়ী logic:

match order_type
  • veg → veg menu

  • nonveg → nonveg menu

  • dessert → dessert menu

  • অন্য কিছু → invalid order

এভাবে match-case ব্যবহার করলে code অনেক পরিষ্কার হয়।


Short Summary

  • Python 3.10 এ introduce হয়েছে match-case statement

  • এটি traditional switch-case এর modern version

  • এটি structural pattern matching ব্যবহার করে

  • complex data structure match করা যায়

  • case _ হলো default case

  • Data science, web development এবং API handling-এ খুব useful