Lesson — Logic
MATCH KARO,
CODE SAADHA.
Ek value, kayi fixed options — UPI status, hafte ka din, menu choice. Aise cases me match-case if-elif se zyada saaf aur MNC-readable hai.
WHY: status check saaf dikhe
UPI payment ka status — SUCCESS, PENDING, FAILED — ya hafte ka din. Aise ek value, kayi fixed options wale cases me match if-elif se zyada readable hai. MNC code review me readability ke marks milte hain.
status = "PENDING"
match status:
case "SUCCESS":
print("Paise pahunch gaye")
case "PENDING":
print("Wait karo")
case "FAILED":
print("Dobara try karo")
case _:
print("Support se baat karo")Ek option match karo: case "SUCCESS": — colon mat bhoolo.
Default catcher — jo kahin match na ho, wahan. Hamesha last me rakho.
2-3 option tak if-elif chalega; usse zyada ho to match saaf rehta hai.
HOW: pattern likhna
match ke neeche har case 4 space indent hota hai. case _ wildcard hai — kuch bhi pakad leta hai, isliye hamesha sabse last. Number, string, dono par kaam karta hai.
day = 3
match day:
case 1:
print("Monday")
case 7:
print("Sunday")
case _:
print("Weekday")Quick check
Koi bhi value pakadne wala default case ka keyword kya hai?
Underscore wala case — match ke end me rakho.
match (3.10+) lambhi if-elif seedhi ko readable banata hai. Naye Python me naye tareeke — updated rehna hi skill hai.http = 404
match http:
case 200:
print("OK")
case 404:
print("Not found")
case _:
print("Other")MNC tip: Status-code example yaad rakho — backend rounds me kaam aayega.
Common mistakes
case _bhoolna — unknown value par kuch print hi nahi hoga.=likhna==ki jagah — match me comparison automatic hai, operator likhna hi nahi.- Indentation galat — case block 4 space andar hota hai.
Ab repetition automate karo — loops ki power dekho.