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.

⏱ 25 min• BeginnerPrerequisite: Lists & conditions

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.

python▶ Run
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.

python▶ Run
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.

Try it: progress counterCounter aur condition change karke observe karo
Run Python dabayein

break aur continue kab?

Quick check

List items ke har item ke liye loop shuru karne wali line likho.

for, temporary variable, in, list name aur ending colon.

🎓
Guido-style insight: for-else Python ka sabse galat-samjha feature: else tab chalta hai jab loop bina break poora ho. Search-success/fail pattern isse ekdum saaf banta hai.
python▶ Run
# Unpythonic: flag variable
found = False
for m in [72, 91, 65]:
    if m > 90:
        found = True
        break

# Pythonic: for-else
for m in [72, 91, 65]:
    if m > 90:
        print("topper mila")
        break
else:
    print("koi topper nahi")

for i, name in enumerate(["Aman", "Priya"]):
    print(i, name)  # index free me

for-else ka flow

LOOPhar item check
→
BREAK?mil gaya to bahar
→
ELSEbreak na hua to chalega

break hua to else skip — search pattern yaad rakho.

MNC tip: enumerate + zip + for-else — teeno ek saath aane chahiye.

Debugging checklist

Repeat ka control samajh aa gaya?

Ab frequently repeated blocks ko named reusable function mein pack karte hain.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course