for Loop and while Loop
for Loop और while Loop
Why Loops?
A loop repeats a block of code many times without writing it again and again. Python has two loops: for (repeat a known number of times) and while (repeat until a condition becomes false).
The for Loop & range()
for repeats over a sequence. range(start, stop, step) generates numbers (stop is not included).
for i in range(1, 6):
print(i, end=" ")range(1, 6)gives 1,2,3,4,5 (6 is excluded).itakes each value one by one.
The while Loop
while keeps running as long as its condition is True. You must change something inside, or it loops forever.
count = 1
while count <= 3:
print("Count:", count)
count += 1 # this line ends the loop eventuallyProgram 1: Print 1 to 10
for i in range(1, 11):
print(i)range(1, 11) covers 1 to 10. Each pass prints one number.
Program 2: Multiplication Table
n = int(input("Table of: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)The loop runs 10 times; each time i grows from 1 to 10 and we print n * i.
Program 3: Sum & Average of N Numbers
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)total = 0starts an accumulator before the loop.total += numadds each input to the running total.
Program 4: Factorial using while
n = int(input("Enter n: "))
fact = 1
while n > 0:
fact *= n
n -= 1
print("Factorial =", fact)5! = 5x4x3x2x1 = 120. Each pass multiplies fact by n, then reduces n.
Program 5: Star Pattern (nested loop)
rows = 5
for i in range(1, rows + 1):
for j in range(i):
print("*", end="")
print()- The outer loop controls rows; the inner loop prints stars in each row.
- Row
iprintsistars, thenprint()moves to a new line.
Program 6: Number Guess with break & continue
secret = 7
while True:
g = int(input("Guess (1-10): "))
if g == secret:
print("Correct!")
break # exit the loop
if g < 1 or g > 10:
print("Out of range, try again")
continue # skip to next loop
print("Wrong, try again")break, continue and loop-else
break— exit the loop immediately.continue— skip the rest of this pass, go to the next.elseon a loop — runs once if the loop finished withoutbreak.
Common Mistakes
- Forgetting to update the variable in a
while→ infinite loop. - Off-by-one with
range()(remember stop is excluded). - Wrong indentation for the loop body.
Practice Tasks
- Print all even numbers from 1 to 50.
- Print the table of any number entered by the user.
- Find the sum of digits of a number using a while loop.
- Print an inverted star triangle (5 stars down to 1).
- Count how many numbers in a list are positive.
Summary
forrepeats a known number of times (often withrange()).whilerepeats until its condition is false — update the variable inside.breakexits,continueskips a pass.- Nested loops make patterns and tables.
Loops क्यों?
Loop code के block को बार-बार लिखे बिना कई बार दोहराता है। Python में दो loops हैं: for (निश्चित बार दोहराना) और while (condition false होने तक दोहराना)।
for Loop और range()
for एक sequence पर दोहराता है। range(start, stop, step) numbers बनाता है (stop शामिल नहीं)।
for i in range(1, 6):
print(i, end=" ")range(1, 6)1,2,3,4,5 देता है (6 बाहर)।iहर value एक-एक करके लेता है।
while Loop
while तब तक चलता है जब तक condition True हो। अंदर कुछ बदलना ज़रूरी है, वरना हमेशा चलता रहेगा।
count = 1
while count <= 3:
print("Count:", count)
count += 1 # यही line अंत में loop खत्म करती हैProgram 1: 1 से 10 Print करें
for i in range(1, 11):
print(i)range(1, 11) 1 से 10 cover करता है। हर pass एक number print करता है।
Program 2: Multiplication Table
n = int(input("Table of: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)Loop 10 बार चलता है; हर बार i 1 से 10 बढ़ता है और हम n * i print करते हैं।
Program 3: N Numbers का Sum और Average
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)total = 0loop से पहले accumulator शुरू करता है।total += numहर input को running total में जोड़ता है।
Program 4: while से Factorial
n = int(input("Enter n: "))
fact = 1
while n > 0:
fact *= n
n -= 1
print("Factorial =", fact)5! = 5x4x3x2x1 = 120। हर pass fact को n से गुणा करता है, फिर n घटाता है।
Program 5: Star Pattern (nested loop)
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मेंistars, फिरprint()नई line पर ले जाता है।
Program 6: break और continue के साथ Number Guess
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")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 से 50 तक सभी even numbers print करें।
- User के दिए किसी number की table print करें।
- while loop से किसी number के digits का sum निकालें।
- उल्टा star triangle print करें (5 stars से 1 तक)।
- एक list में कितने numbers positive हैं, गिनें।
सारांश
forनिश्चित बार दोहराता है (अक्सरrange()के साथ)।whilecondition false होने तक दोहराता है — अंदर variable update करें।breakबाहर निकलता है,continueएक pass छोड़ता है।- Nested loops patterns और tables बनाते हैं।