Lesson 07 � Logic
REPEAT KARO.
SOCH SAMJH KAR.
Ek student ka result print karna easy hai. 500 students ka✓ Same instruction ko manually copy karna bad idea hai. Loop ek controlled repeat mechanism hai�aur control hi important word hai.
FOR: collection ke har item par kaam
Jab aapke paas list, string ya kisi ordered sequence ke items hain, for natural choice hai. Loop variable har turn mein current item hold karta hai.
skills = ["Python", "SQL", "Pandas"]
for skill in skills:
print("Learning:", skill)Read it literally: �skills mein har skill ke liye, Learning + skill print karo.� skill variable sirf loop body ke context mein current value ko describe kar raha hai.
range(): fixed number of repeats
range(3) values 0, 1, 2 deta hai. End value included nahi hoti. Count ka intent clear ho to range(1, 4) readable ho sakta hai�1, 2, 3.
for day in range(1, 4):
print(f"Day {day}: practice complete")WHILE: jab end condition se decide ho
while tab tak run karega jab tak condition true hai. Ismein update line mandatory socho�warna condition kabhi false nahi hogi aur infinite loop ban sakta hai.
break aur continue kab?
breakloop ko immediately stop karta hai�example: required record mil gaya.continuecurrent turn skip karta hai aur next item par jaata hai�example: blank row ignore karni hai.- In dono ko only clear purpose ke liye use karo; nested logic se escape karne ke liye nahi.
Quick check
List items ke har item ke liye loop shuru karne wali line likho.
for, temporary variable, in, list name aur ending colon.
Debugging checklist
- Loop body correctly indented hai?
whilecondition ko false banane wala update har turn mein ho raha hai?- Index manually use karna zaruri hai ya direct
for item in itemsbetter hai? rangeka last value exclude hota hai�off-by-one errors yahin se aate hain.
Ab frequently repeated blocks ko named reusable function mein pack karte hain.