🟢 Beginner  ·  Lesson 09

for Loop और while Loop

Loops क्यों?

Loop code के block को बार-बार लिखे बिना कई बार दोहराता है। Python में दो loops हैं: for (निश्चित बार दोहराना) और while (condition false होने तक दोहराना)।

for Loop और range()

for एक sequence पर दोहराता है। range(start, stop, step) numbers बनाता है (stop शामिल नहीं)।

Python – range.py
for i in range(1, 6):
    print(i, end=" ")
1 2 3 4 5
  • range(1, 6) 1,2,3,4,5 देता है (6 बाहर)।
  • i हर value एक-एक करके लेता है।

while Loop

while तब तक चलता है जब तक condition True हो। अंदर कुछ बदलना ज़रूरी है, वरना हमेशा चलता रहेगा।

Python – while.py
count = 1
while count <= 3:
    print("Count:", count)
    count += 1   # यही line अंत में loop खत्म करती है
Count: 1 Count: 2 Count: 3

Program 1: 1 से 10 Print करें

Python – ten.py
for i in range(1, 11):
    print(i)
1 2 3 ... 10

range(1, 11) 1 से 10 cover करता है। हर pass एक number print करता है।

Program 2: Multiplication Table

Python – table.py
n = int(input("Table of: "))
for i in range(1, 11):
    print(n, "x", i, "=", n * i)
Table of: 5 5 x 1 = 5 5 x 2 = 10 ... 5 x 10 = 50

Loop 10 बार चलता है; हर बार i 1 से 10 बढ़ता है और हम n * i print करते हैं।

Program 3: N Numbers का Sum और Average

Python – sumavg.py
n = int(input("How many numbers? "))
total = 0
for i in range(n):
    num = int(input("Enter number: "))
    total += num

print("Sum     =", total)
print("Average =", total / n)
How many numbers? 3 Enter number: 10 Enter number: 20 Enter number: 30 Sum = 60 Average = 20.0
  • total = 0 loop से पहले accumulator शुरू करता है।
  • total += num हर input को running total में जोड़ता है।

Program 4: while से Factorial

Python – factorial.py
n = int(input("Enter n: "))
fact = 1
while n > 0:
    fact *= n
    n -= 1
print("Factorial =", fact)
Enter n: 5 Factorial = 120

5! = 5x4x3x2x1 = 120। हर pass fact को n से गुणा करता है, फिर n घटाता है।

Program 5: Star Pattern (nested loop)

Python – stars.py
rows = 5
for i in range(1, rows + 1):
    for j in range(i):
        print("*", end="")
    print()
* ** *** **** *****
  • बाहरी loop rows control करता है; भीतरी loop हर row में stars print करता है।
  • Row i में i stars, फिर print() नई line पर ले जाता है।

Program 6: break और continue के साथ Number Guess

Python – guess.py
secret = 7
while True:
    g = int(input("Guess (1-10): "))
    if g == secret:
        print("Correct!")
        break          # loop से बाहर
    if g < 1 or g > 10:
        print("Out of range, try again")
        continue       # अगले loop पर जाएं
    print("Wrong, try again")
Guess (1-10): 3 Wrong, try again Guess (1-10): 7 Correct!

break, continue और loop-else

  • break — loop तुरंत छोड़ दें।
  • continue — इस pass का बाकी हिस्सा छोड़कर अगले पर जाएं।
  • loop पर else — अगर loop बिना break पूरा हुआ तो एक बार चलता है।

सामान्य गलतियाँ

  • while में variable update करना भूलना → infinite loop।
  • range() में off-by-one (याद रखें stop बाहर है)।
  • loop body के लिए गलत indentation।

Practice Tasks

  1. 1 से 50 तक सभी even numbers print करें।
  2. User के दिए किसी number की table print करें।
  3. while loop से किसी number के digits का sum निकालें।
  4. उल्टा star triangle print करें (5 stars से 1 तक)।
  5. एक list में कितने numbers positive हैं, गिनें।

सारांश

  • for निश्चित बार दोहराता है (अक्सर range() के साथ)।
  • while condition false होने तक दोहराता है — अंदर variable update करें।
  • break बाहर निकलता है, continue एक pass छोड़ता है।
  • Nested loops patterns और tables बनाते हैं।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.