Theory
College Canteen में चुनाव
कल्पना कीजिए अपने college canteen counter पर एक गरम samosa की तलब के साथ खड़े होना। अपना order देने से पहले, आपका दिमाग़ अपने-आप एक त्वरित जाँच करता है: क्या मेरे wallet में काफ़ी पैसे हैं? अगर हाँ, ख़रीदिए। अगर नहीं, छोड़िए। आपके Semester 1 BCA104 C labs में, इन चुनावों को लिखने के लिए अंतहीन parentheses और curly braces चाहिए थे। Python इन बिल्कुल, असली दुनिया के फ़ैसलों को brackets में डूबे बिना साफ़ और पठनीय कैसे दिखाता है?
Theory
Railway Track Switch
conditional statements को एक railway track switcher की तरह सोचिए। एक train एक अकेले track पर आगे बढ़ती है। जब यह एक junction पर पहुँचती है, lever की position तय करती है कि train बाएँ Platform 1 की ओर मुड़ती है या दाएँ Platform 2 की ओर switch करती है। train कभी एक साथ दोनों रास्तों पर नहीं जा सकती। lever की position एक true या false condition दर्शाती है जो आपके program को बिल्कुल एक track पर route करती है।
Theory
Decision Elements औपचारिक रूप से
Python में, conditional statements expressions को true या false के रूप में evaluate करके execution के flow को नियंत्रित करते हैं। if statement प्राथमिक gate mark करता है। elif (else if का संक्षिप्त) कई वैकल्पिक conditions को क्रमिक रूप से सँभालता है, पारंपरिक C switch-case block की जगह लेते हुए। else clause सारे बाक़ी cases पकड़ता है जब हर पिछली condition विफल होती है। गहरे logic checks जोड़ने के लिए, आप एक conditional block को सीधे दूसरे के अंदर embed करके एक nested if structure लिख सकते हैं।
At a glance
Table 1: Python conditional statement किस्में और design use cases।
| Structure Style | Keyword Pattern | इसे कब इस्तेमाल करें |
|---|---|---|
| Simple Condition | if | जब आप एक अकेले true outcome के लिए एक block चलाना चाहते हैं। |
| Alternative Choice | if-else | जब आपके पास बिल्कुल दो रास्ते हैं, एक true और एक false। |
| Multi-way Branch | if-elif-else | जब आपको क्रम में कई conditions test करनी हों। |
| Layered Checks | nested if | जब एक secondary condition एक primary condition के pass होने पर निर्भर हो। |
Theory
Worked Example: Smart Expense Alerts
आइए अपने चलते project, PocketMoney के लिए एक पूर्ण tiered conditional tree लागू करें। हम अपना मौजूदा account balance जाँचेंगे और अलग resource thresholds के आधार पर customized financial health status messages print करेंगे।
Practical
PocketMoney Financial Status Routing
balance = 120
if balance > 500:
print("Safe Zone: Stable balance.")
elif balance >= 100:
print("Warning Zone: Control your spends!")
if balance == 120:
print("Alert: Exact budget match detected.")
else:
print("Critical Zone: Overspent completely!")This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Conditional Path को ट्रेस करें
ऊपर के Python script का मन में विश्लेषण कीजिए। 120 के एक balance value के साथ, terminal पर कौन सी बिल्कुल printed lines भेजी जाएँगी? tap करने से पहले चरण ट्रेस कीजिए।
Show the answer
script output करेगी:
Warning Zone: Control your spends!
Alert: Exact budget match detected.
क्यों? Python line 3 (120 > 500) को False के रूप में evaluate करता है, तो यह पहला block skip करता है। यह line 5 (120 >= 100) पर जाता है, जो True evaluate होती है। यह इस block में घुसता है, warning message print करता है, और फिर line 7 (120 == 120) पर nested if से टकराता है। चूँकि वह भी True है, यह alert output करता है। else block पूरी तरह अनदेखा किया जाता है।
Quiz
BCA204 Python और BCA104 C में conditional routing के बीच एक बड़ा संरचनात्मक अंतर क्या है?
- Python को condition statements के इर्द-गिर्द parentheses चाहिए।
- Python curly braces पूरी तरह छोड़ देता है और एक trailing colon से mark किए सख़्त indentation blocks इस्तेमाल करता है।
- Python एक अकेले if block से जुड़े कई else blocks की अनुमति देता है।
- Python में एक elif keyword नहीं है, developers को switch-case tools इस्तेमाल करने पर मजबूर करते हुए।
Show the answer
Python curly braces पूरी तरह छोड़ देता है और एक trailing colon से mark किए सख़्त indentation blocks इस्तेमाल करता है।
Python अनिवार्य condition parentheses और grouping curly braces को पूरी तरह ख़त्म कर देता है। बजाय इसके, यह condition line के अंत में एक trailing colon character पर निर्भर करता है, nested code statements के लिए एक जैसे indentation spaces के साथ मिलकर।
Quiz
इस समस्याग्रस्त code fragment पर विचार कीजिए: balance = 600 if balance > 100: print("Medium Balance") elif balance > 500: print("High Balance") execution पर क्या print होगा?
- Medium Balance
- High Balance
- दोनों statements print होंगे।
- SyntaxError: overlapping conditions
Show the answer
Medium Balance
Python conditional structures को ऊपर से नीचे evaluate करता है। जिस पल यह एक मेल खाती condition पाता है जो True देती है, यह उस ख़ास block को execute करता है और तुरंत पूरी statement structure से बाहर निकलता है। क्योंकि 600 > 100 True है, line 3 चलती है और बाद वाला elif पूरी तरह skip हो जाता है।
Watch out
Classic जाल: Colon Collapse
university exam papers में सबसे बार-बार marks-गँवाने वाली ग़लती if balance < 100 जैसे expressions लिखना और line के अंत में अनिवार्य colon character : जोड़े बिना अगली line पर जाना है। Python engine इस punctuation token से एक execution block खोलने की उम्मीद करता है। इसे छोड़ना किसी भी code के execute होने से पहले एक runtime SyntaxError trigger करता है।
Theory
Conditions को Semester 3 से जोड़ना
Conditional checks सुरक्षित database interaction के द्वारपाल हैं। Semester 3 (BCA303) में, जब SQLite tables द्वारा powered dynamic applications बनाते हैं, आप alterations commit करने से पहले text constraints validate करने के लिए conditional lines इस्तेमाल करेंगे, जैसे if user_role == "Admin": execute_db_delete()।
Summary
Key takeaways
- Conditional structures expressions को evaluate करके execution flow dynamically route करती हैं।
- elif keyword क्रमिक multi-way testing देता है, C switch-case की जगह लेते हुए।
- Python पहला True block execute करने के तुरंत बाद expression matching समाप्त करता है।
- Nested loops और nested conditional blocks को सख़्त, एकसमान spacing levels चाहिए।
- Trailing colons सारे choice keywords के लिए अनिवार्य संरचनात्मक घटक हैं।
- Memory Hook: Colons blocks खोलते हैं, indentation tracks नियमित करता है!