for loop, range, break, continue, pass, else with for, nested for loop

Python for loops સીધા elements કે sequence ranges પર iterate કરે છે, break, continue, pass, અને એક અનોખા else block સાથે શક્તિશાળી નિયંત્રણ આપતાં.

12 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


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 StructureCode ની અંદર Functional ImpactPocketMoney Tracking Use Case
range(start, stop, step)stop value સુધી પણ એને છોડીને integers generate કરે છે.range(1, 31, 7) weekly dates track કરે છે.
passNull operation placeholder જે blocks ને valid રાખે છે.એક અલિખિત category filter માટે placeholder.
for...elseelse 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 શું છે?

  1. [2, 5, 8, 11]
  2. [2, 5, 8]
  3. [5, 8, 11]
  4. [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 વગર સંપૂર્ણપણે ખાલી છોડે?

  1. Python blank space અવગણે છે અને આગળની line સરળતાથી ચલાવે છે.
  2. script એક તરત IndentationError કે SyntaxError ફેંકે છે.
  3. Python આપોઆપ એક break statement નાખે છે.
  4. 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 સંપૂર્ણપણે ભાગે છે!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Conditions, Loops and Strings

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

for loop, range, break, continue, pass, else with for, nested for loop · Programming Skills · Gri-Learn