Table of Contents
Toggle
Python में Loop क्या है? Loop एक programming concept है जिसका उपयोग code को बार-बार execute करने के लिए किया जाता है। जब तक कोई condition true रहती है, loop चलता रहता है और same काम बार-बार करता है।
अगर आपको कोई काम 100 बार करना है, तो 100 बार code लिखने की जरूरत नहीं होती — आप एक loop का उपयोग करके वही काम आसानी से कर सकते हैं। इसलिए loop Python programming का एक बहुत important concept है।
for variable in sequence:
# code block
While Loop:
while condition:
# code block
for i in range(1,6):
print(i)
💡 Loop समझना Python सीखने की foundation है। अब आगे For Loop Examples देखें।
Python में For Loop क्या है? For loop एक ऐसा loop है जो किसी sequence (जैसे list, string, tuple या range) के हर element पर एक-एक करके code run करता है। यह तब उपयोग किया जाता है जब हमें पहले से पता होता है कि loop कितनी बार चलेगा।
for variable in sequence:
# code block
उदाहरण:
for i in range(5):
print(i)
for i in range(1,6):
print(i)
🚀 अब जब आपने For Loop समझ लिया है, आगे 20+ For Loop Examples देखें।
यहाँ आप Python for loop examples सीखेंगे जो beginner से advanced level तक हैं। हर example के साथ code, output और real-life use case दिया गया है जिससे आप आसानी से समझ सकें कि Python में for loop कैसे काम करता है।
for i in range(5):
print(i)
Output: 0 1 2 3 4
for i in range(1,11):
print(i)
Output: 1 से 10
for i in range(2,11,2):
print(i)
Output: 2 4 6 8 10
fruits = ['apple','banana','mango']
for f in fruits:
print(f)
Output: apple banana mango
for ch in "Python":
print(ch)
Output: P y t h o n
total = 0
for i in range(1,6):
total += i
print(total)
Output: 15
n = 5
for i in range(1,11):
print(n*i)
Output: 5 10 15 …
for i in range(5,0,-1):
print(i)
Output: 5 4 3 2 1
fruits=['a','b']
for i,v in enumerate(fruits):
print(i,v)
Output: 0 a 1 b
for i in range(2):
for j in range(2):
print(i,j)
Output: pairs
for i in range(5):
if i==3:
break
print(i)
Output: 0 1 2
for i in range(5):
if i==2:
continue
print(i)
Output: 0 1 3 4
for i in range(5):
print(i*i)
Output: 0 1 4 9 16
for i in range(10):
if i%2!=0:
print(i)
Output: 1 3 5 7 9
count=0
for ch in "hello":
count+=1
print(count)
Output: 5
nums=[1,2,3]
s=0
for n in nums:
s+=n
print(s)
Output: 6
nums=[3,7,2]
m=nums[0]
for n in nums:
if n>m:
m=n
print(m)
Output: 7
fact=1
for i in range(1,6):
fact*=i
print(fact)
Output: 120
for i in range(5):
print("*"*i)
Output: *
d={"a":1,"b":2}
for k,v in d.items():
print(k,v)
Output: a 1 b 2
🚀 अब आगे While Loop Examples सीखें और अपनी coding skills को improve करें।
Python में While Loop क्या है? While loop एक ऐसा loop है जो तब तक चलता रहता है जब तक दी गई condition true रहती है। जैसे ही condition false होती है, loop रुक जाता है।
For loop के विपरीत, while loop तब उपयोग किया जाता है जब हमें पहले से यह पता नहीं होता कि loop कितनी बार चलेगा। यह condition के आधार पर काम करता है।
while condition:
# code block
उदाहरण:
i = 1
while i <= 5:
print(i)
i += 1
🚀 अब आगे While Loop Examples देखें और practice करें।
यहाँ आप while loop programs in Python सीखेंगे — beginner से advanced level तक। हर example के साथ code, output और use case दिया गया है जिससे आपका logic strong होगा।
i=1
while i<=5:
print(i)
i+=1
Output: 1 2 3 4 5
i=1
s=0
while i<=5:
s+=i
i+=1
print(s)
Output: 15
i=2
while i<=10:
print(i)
i+=2
Output: 2 4 6 8 10
num=123
rev=0
while num>0:
d=num%10
rev=rev*10+d
num//=10
print(rev)
Output: 321
i=1
fact=1
while i<=5:
fact*=i
i+=1
print(fact)
Output: 120
num=1234
count=0
while num>0:
num//=10
count+=1
print(count)
Output: 4
n=3
i=1
while i<=10:
print(n*i)
i+=1
num=1
while num>=0:
num=int(input())
while True:
print("Running")
i=1
while True:
if i==5:
break
print(i)
i+=1
i=1
while i<=10:
if i%2!=0:
print(i)
i+=1
num=123
s=0
while num>0:
s+=num%10
num//=10
print(s)
a,b=0,1
i=0
while i<5:
print(a)
a,b=b,a+b
i+=1
n=7
i=2
flag=0
while i
num=121
temp=num
rev=0
while num>0:
d=num%10
rev=rev*10+d
num//=10
print(temp==rev)
base=2
exp=3
res=1
while exp>0:
res*=base
exp-=1
print(res)
num=5
guess=0
while guess!=num:
guess=int(input())
i=5
while i>0:
print(i)
i-=1
total=0
num=1
while num!=0:
num=int(input())
total+=num
print(total)
i=1
while i<=2:
j=1
while j<=2:
print(i,j)
j+=1
i+=1
🚀 Practice more questions here:
👉 While Loop Practice QuestionsFor loop और while loop दोनों का उपयोग Python में code को बार-बार चलाने के लिए किया जाता है। लेकिन दोनों का उपयोग अलग-अलग situations में होता है — यह इस बात पर depend करता है कि iterations पहले से पता हैं या नहीं।
| Feature | For Loop | While Loop |
|---|---|---|
| Iterations | Known (पहले से पता) | Unknown (Condition पर depend) |
| Condition | Implicit | Explicit |
| Usage | Sequence-based | Condition-based |
| Control | Automatic | Manual |
| Risk | Low | High (Infinite Loop) |
🚀 अब आपने Python loops पूरी तरह समझ लिए हैं — अब practice करें और coding skills improve करें।
यहाँ Python loops (for loop और while loop) से जुड़े सबसे common सवाल और उनके आसान जवाब दिए गए हैं।
for i in range(5):
print(i)
Output: 0 1 2 3 4
For Loop और While Loop पर अपनी understanding test करें। सही answer चुनें और Submit करें।
Explore more Python tutorials and deepen your understanding with these helpful guides.
🚀 Keep learning and master Python step by step with real-world examples.
