Python Match–Case Statements & eval() Notes
Master Pattern Matching (Match-Case) in Python along with a deep-dive breakdown of the eval() function, syntax, and real-life examples.
Author: By Instructor
1. What is Match-Case in Python?
The match-case statement is used to match a variable's value against multiple potential case patterns. When a matching pattern is found, the corresponding code block executes.
Easy English Explanation
- Pattern Matching: Evaluates a target variable and branches execution based on matching value.
- Replaces If-Elif: Cleaner and more readable alternative to long if-elif-else chains.
- Default Catch-all: Uses case _ as the fallback default block.
Hinglish Explanation
- Match-Case Ka Use: Match-case ka use ek variable ko multiple values se match karne ke liye hota hai.
- Execution: Jo value match hoti hai, uska code run ho jata hai.
- Default Case: Agar koi value match na ho, to case _ (default case) run hota hai.
2. Basic Syntax of Match-Case
match variable:
case value1:
code
case value2:
code
case value3:
code
case _:
default_code
Keyword Roles
- match: Evaluates the variable.
- case: Checks specific target value.
- case _: Default fallback case when no earlier case matches.
Role Explanation
- match → variable check karta hai.
- case → specific value match karta hai.
- case _ → default case (agar koi match na ho).
3. Practical Match-Case Examples
Example 1: Day Checking
Python Code - Day Checker
day = 1
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case _:
print("Invalid day")
Output
MondayExample 2: Activity Tracker
Python Code - Activity Tracker
activity_choice = 2
match activity_choice:
case 1:
print("yes padhai kr rhe hai")
case 2:
print("game khel rhe hai")
case 3:
print("movie dekh rhe hai")
case 4:
print("ghumne gye hai")
case _:
print("velle bethe hue")
Output
game khel rhe hai5. SPECIAL DEEP DIVE: What is the Purpose of eval() in Python?
Note on eval() Function
Definition: The eval() function stands for "Evaluate". It evaluates a string argument dynamically as a Python expression and returns the calculated result.
Why Use eval()? (Purpose)
- Auto Type Parsing: When taking user input via input(), Python returns a string. Using eval(input()) automatically parses numeric strings into integers or floats without needing explicit int() or float() conversion!
- Dynamic Math Evaluation: Can evaluate math expressions stored as strings, e.g. eval("10 + 20") returns 30.
- Convenience: Handles integers, floats, lists, and tuples dynamically from string inputs.
eval() ka Purpose (Simple Words me)
- Auto Conversion: input() hamesha String data deta hai. Agar hum eval(input()) use karte hain, to Python automatically integer ("10" -> 10) ya float ("10.5" -> 10.5) samajh kar convert kar deta hai.
- Calculation: Direct string math expression ko solve kar deta hai (jaise eval("5 * 4") -> 20).
- No int/float hassle: Alag se int() ya float() nahi likhna padta.
Python Code - Demonstration of eval()
expr_result = eval("10 + 20 * 2")
print("Evaluated Expression:", expr_result)
val = eval("45")
print("Parsed Value:", val, "Type:", type(val))
Output
Evaluated Expression: 50Parsed Value: 45 Type: <class 'int'>
6. Match-Case Combined with eval() Input
Python Code - Match-Case using eval()
num = eval("2")
match num:
case 1:
print("You entered One")
case 2:
print("You entered Two")
case 3:
print("You entered Three")
case _:
print("Invalid number")
Output
You entered TwoLine-by-Line Breakdown
| Line | Code Snippet | Easy English Explanation | Hinglish Explanation |
|---|---|---|---|
| 1 | num = eval("2") | Evaluates the string "2" and converts it automatically to integer 2. | eval() string "2" ko automatic int value 2 banata hai. |
| 3 | match num: | Begins pattern matching on variable num. | Variable num par matching start hoti hai. |
| 6-7 | case 2: print("You entered Two") | Matches value 2 and prints "You entered Two". | Case 2 match hote hi "You entered Two" print karta hai. |
7. Quick Revision Summary
Summary Points
- match: Evaluates the target variable.
- case: Defines value pattern and action.
- case _: Default fallback option.
- eval(): Evaluates string expressions and auto-parses numbers.
Quick Summary
- match → check karta hai.
- case → action perform karta hai.
- case _ → default fallback option.
- eval() → string data ko evaluate aur auto-convert karta hai.