Theory
Fixed Checklist पहेली
आपके Semester 1 BCA104 C labs में, 1 से 10 तक numbers print करने के लिए एक loop counter set up करना, conditions जाँचना, और for(int i=1; i<=10; i++) की तरह values को हाथ से increment करना पड़ता था। अगर आप increment करना भूल जाते, आपका system एक अनंत cycle में crash हो जाता। Python इस जटिल index-management math को पूरी तरह क्यों छोड़ देता है, आपको साफ़, वाक्य-जैसी पठनीयता के साथ data arrays या numeric ranges में sweep करने देते हुए?
Theory
Attendance Register
एक Python for loop को एक class professor की तरह सोचिए जो एक attendance register में names पर tick लगाता है। professor student desks के बीच की दूरी calculate नहीं करता: वे बस list पर पहले student की ओर इशारा करते हैं, क्रमिक रूप से दूसरे पर जाते हैं, और आख़िरी नाम पहुँचने पर अपने-आप रुक जाते हैं। range() function एक automated roll-call generator की तरह काम करता है, एक fixed step pattern के आधार पर ख़ास numbers एक-एक करके पुकारते हुए।
Theory
Definite Iteration औपचारिक रूप से
एक for loop एक collection-controlled iteration structure है जो एक list, string, या range जैसे एक sequence में क़दम रखकर एक block code को पूर्वनिर्धारित बार execute करता है। range() function integers का एक immutable sequence dynamically generate करता है। Python loops में अनोखे modifiers शामिल हैं: break तुरंत बाहर निकलता है, continue मौजूदा बारी skip करता है, pass एक blank syntactic placeholder के रूप में काम करता है, और अनोखा else with for block केवल तब execute होता है जब loop एक break से टकराए बिना पूरी तरह पूरा होता है।
At a glance
Table 1: Python में definite iteration tools और संरचनात्मक modifiers।
| Loop Keyword Structure | Code के अंदर Functional Impact | PocketMoney Tracking Use Case |
|---|---|---|
| range(start, stop, step) | stop value तक पर उसे छोड़कर integers generate करता है। | range(1, 31, 7) weekly dates track करता है। |
| pass | Null operation placeholder जो blocks को valid रखता है। | एक अलिखित category filter के लिए placeholder। |
| for...else | else block केवल तब execute करता है जब कोई break trigger न हो। | audit में शून्य fraudulent logs मिले यह पुष्टि करता है। |
| nested for | हर outer loop step के लिए एक inner loop पूरी तरह चलाता है। | weeks और daily spends पर iterate करता है। |
Theory
Worked Example: Weekly Ledger का Audit
आइए अपने PocketMoney tracker के लिए एक multi-layered verification system लागू करें। हम daily transactions के एक sequence में loop करना चाहते हैं, zero-value entries skip करने के लिए continue इस्तेमाल करना चाहते हैं, future development के लिए pass इस्तेमाल करना चाहते हैं, और एक for-else structure इस्तेमाल करके एक critical emergency overspent limit जाँचना चाहते हैं।
Practical
PocketMoney Definite Iteration Auditor
daily_spends = [40, 0, 150, 200, 0, 30]
limit_breached = False
for spend in daily_spends:
if spend == 0:
continue
if spend > 150:
print("Audit Alert: Suspicious transaction detected!")
break
if spend == 40:
pass
else:
print("Audit Clean: All spends are within bounds.")This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Control Flow नतीजे को ट्रेस करें
ऊपर के Python script का मन में विश्लेषण कीजिए। terminal पर कौन से बिल्कुल messages भेजे जाएँगे? tap करने से पहले हर index condition ध्यान से ट्रेस कीजिए।
Show the answer
script output करेगी:
Audit Alert: Suspicious transaction detected!
क्यों? sequence ट्रेस करते हैं। index 0 40 है (pass से टकराता है, continue करता है)। index 1 0 है (continue से टकराता है, ऊपर skip करता है)। index 2 150 है (150 से ज़्यादा नहीं, अनदेखा)। index 3 200 है (150 से ज़्यादा, alert print करता है और break से टकराता है)। क्योंकि break trigger होता है, पूरा loop तुरंत समाप्त होता है, और for-else block पूरी तरह skip होता है।
Quiz
एक university theory exam में expression list(range(2, 11, 3)) का बिल्कुल output sequence क्या है?
- [2, 5, 8, 11]
- [2, 5, 8]
- [5, 8, 11]
- [3, 6, 9]
Show the answer
[2, 5, 8]
range function 2 से शुरू होता है और 3 के एक step से increment करता है, 2, 5, और 8 देते हुए। यह 11 की upper limit पहुँचने या पार करने से तुरंत पहले रुकता है। इसलिए, 11 अंतिम list sequence से बाहर रखा जाता है।
Quiz
क्या होता है अगर एक programmer एक itemized loop block को बिना किसी code या placeholders के पूरी तरह ख़ाली छोड़ता है?
- Python blank space अनदेखा करता है और अगली line सहजता से चलाता है।
- script एक तुरंत IndentationError या SyntaxError फेंकती है।
- Python अपने-आप एक break statement डालता है।
- compiler block को शून्य values से भरता है।
Show the answer
script एक तुरंत IndentationError या SyntaxError फेंकती है।
Python संरचनात्मक रूप से ख़ाली blocks नहीं सँभाल सकता। अगर आप एक colon पर ख़त्म होती एक loop header line लिखते हैं पर नीचे कोई block code नहीं देते, interpreter crash होता है। errors रोकने के लिए आपको एक संरचनात्मक placeholder के रूप में pass keyword इस्तेमाल करना होगा।
Watch out
Classic जाल: Stop-Value Exclusion
university laboratory examinations में सबसे बार-बार marks-गँवाने वाली ग़लती यह मानना है कि range(1, 5) 1 से 5 तक पाँच बार loop करता है। यह नहीं करता। Python range sequences ऊपर open-ended हैं: वे start integer शामिल करते हैं पर stop integer से सख़्ती से पहले समाप्त होते हैं। range(1, 5) लिखना केवल indices 1, 2, 3, और 4 process करता है। 5 शामिल करने के लिए, आपका stop value 6 लिखा जाना चाहिए।
Theory
Loops को Semester 3 से जोड़ना
Definite loops row data manipulation के लिए मूल mechanism हैं। Semester 3 (BCA303) में, जब एक database connector cursor इस्तेमाल करके एक SQLite database dataset से संरचनात्मक tuples unpack करते हैं, आप multi-column rows को कुशलता से singular application entities में parse करने के लिए for row in database_cursor: जैसे expressions पर निर्भर करेंगे।
Summary
Key takeaways
- For loops collection groupings के अलग members में क्रमिक रूप से iterate करते हैं।
- range function अंतिम limit bound को छोड़कर mathematical integer progressions generate करता है।
- break statement loops से तुरंत बाहर निकलता है, किसी भी साथ वाले else clauses को bypass करते हुए।
- continue modifier सक्रिय pass रोकता है और अगली header value entry पर जाता है।
- Pass एक code placeholder के रूप में काम करता है ख़ाली conditional या संरचनात्मक loops को valid रखते हुए।
- Memory Hook: Loop पूरे होने पर else trigger, breaks पूरी तरह भागते हैं!