Python Loops क्या हैं? for Loop, while Loop, range(), break & continue | RBSE Class 12
Python Loop क्या है?
Programming में कई बार हमें एक ही काम को बार-बार करना पड़ता है। यदि हम हर बार वही code अलग-अलग लिखें तो program unnecessarily लंबा हो जाएगा। ऐसी स्थिति में Loop का उपयोग किया जाता है।
Loop की मदद से किसी statement या statements के block को आवश्यकता के अनुसार बार-बार execute किया जा सकता है।
Loop की आवश्यकता क्यों होती है?
मान लीजिए हमें 1 से 5 तक numbers print करने हैं। बिना loop के हमें बार-बार print() लिखना पड़ेगा।
print(1)
print(2)
print(3)
print(4)
print(5)
लेकिन loop का उपयोग करके यही काम बहुत कम code में किया जा सकता है।
for i in range(1, 6):
print(i)
Python में Loops के प्रकार
| Loop | मुख्य उपयोग |
|---|---|
| for loop | Sequence या iterable के elements पर iteration करने के लिए |
| while loop | Condition True रहने तक statements execute करने के लिए |
1. Python for Loop
for loop का उपयोग किसी sequence या iterable के elements पर एक-एक करके iteration करने के लिए किया जाता है।
Syntax
for variable in sequence:
statement
Simple Example
for i in [1, 2, 3, 4, 5]:
print(i)
Output:
1
2
3
4
5
for Loop with range()
Python में numbers की sequence generate करने के लिए range() का बहुत अधिक उपयोग किया जाता है।
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
range() में stop value सामान्यतः शामिल नहीं होती।
इसलिए range(1, 6) में 1 से 5 तक values मिलती हैं।
range() Function क्या है?
range() एक sequence-like range object प्रदान करता है, जिसका उपयोग loops में numbers की series पर iteration करने के लिए बहुत सामान्य रूप से किया जाता है।
range(stop)
for i in range(5):
print(i)
Output:
0
1
2
3
4
जब केवल एक argument दिया जाता है तो counting सामान्यतः 0 से शुरू होती है।
range(start, stop)
for i in range(2, 6):
print(i)
Output:
2
3
4
5
range(start, stop, step)
तीसरा argument step बताता है कि values के बीच कितना increment या decrement होगा।
for i in range(2, 11, 2):
print(i)
Output:
2
4
6
8
10
Reverse Counting with range()
Negative step का उपयोग करके reverse direction में iteration किया जा सकता है।
for i in range(10, 0, -1):
print(i)
Output:
10
9
8
7
6
5
4
3
2
1
1 से 10 तक Numbers का Sum
total = 0
for i in range(1, 11):
total = total + i
print("Sum =", total)
Output:
Sum = 55
Even Numbers Print करना
for i in range(2, 21, 2):
print(i)
यह program 2 से 20 तक even numbers print करता है।
Odd Numbers Print करना
for i in range(1, 20, 2):
print(i)
Python List पर for Loop
fruits = ["Apple", "Banana", "Mango", "Orange"]
for fruit in fruits:
print(fruit)
Output:
Apple
Banana
Mango
Orange
String पर for Loop
String के characters पर भी iteration किया जा सकता है।
word = "Python"
for ch in word:
print(ch)
Output:
P
y
t
h
o
n
2. Python while Loop
while loop तब तक statements execute करता है जब तक उसकी condition True रहती है।
Syntax
while condition:
statement
Simple Example
i = 1
while i <= 5:
print(i)
i = i + 1
Output:
1
2
3
4
5
while Loop से 1 से 10 तक Numbers
i = 1
while i <= 10:
print(i)
i += 1
while Loop से Even Numbers
i = 2
while i <= 20:
print(i)
i += 2
for Loop और while Loop में अंतर
| for Loop | while Loop |
|---|---|
| Sequence या iterable पर iteration के लिए बहुत उपयोगी है। | Condition-based repetition के लिए उपयोगी है। |
| range() के साथ numeric iteration आसानी से किया जा सकता है। | Condition True रहने तक चलता है। |
| Iteration count अक्सर स्पष्ट होता है। | Iterations की संख्या condition और updates पर निर्भर हो सकती है। |
Nested Loop क्या है?
जब एक loop के अंदर दूसरा loop लिखा जाता है तो उसे Nested Loop कहा जाता है।
Example
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
यहाँ outer loop के प्रत्येक iteration पर inner loop चलता है।
Multiplication Table का Program
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Example Output
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
break Statement
break statement का उपयोग loop को तुरंत terminate करने के लिए किया जाता है।
Example
for i in range(1, 11):
if i == 5:
break
print(i)
Output:
1
2
3
4
जब i == 5 हुआ, break ने loop को समाप्त कर दिया।
continue Statement
continue statement current iteration को skip करके loop की अगली iteration पर चला जाता है।
Example
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
यहाँ 3 वाली iteration skip हो गई।
break और continue में अंतर
| break | continue |
|---|---|
| पूरे loop को terminate करता है। | Current iteration को skip करता है। |
| Loop के बाहर control चला जाता है। | Loop की अगली iteration शुरू होती है। |
Loop में else
Python में loops के साथ else block का भी उपयोग किया जा सकता है। यदि loop सामान्य रूप से समाप्त होता है और break से terminate नहीं होता, तो else block execute हो सकता है।
Example
for i in range(1, 6):
print(i)
else:
print("Loop completed")
Prime Number Check करने का Basic Program
num = int(input("Enter a number: "))
if num < 2:
print("Not Prime")
else:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print("Prime Number")
else:
print("Not Prime")
Factorial Program using Loop
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial =", factorial)
List के सभी Numbers का Sum
numbers = [10, 20, 30, 40, 50]
total = 0
for num in numbers:
total += num
print("Total =", total)
Nested Loop से Pattern
for i in range(1, 5):
for j in range(i):
print("*", end="")
print()
Output:
*
**
***
****
Common Loop Mistakes
1. while Loop में Variable Update भूलना
while loop में counter update नहीं करने पर condition लंबे समय तक True रह सकती है।
2. range() की Stop Value भूलना
range(1, 5)
यह 1, 2, 3, 4 तक values देता है; 5 को include नहीं करता।
3. Indentation गलत करना
Loop के अंदर का code सही indentation के साथ होना चाहिए।
4. break और continue को confuse करना
break loop को समाप्त करता है, जबकि continue current iteration को skip करता है।
Python Loops का Quick Revision
| Concept | मुख्य बात |
|---|---|
| for | Iterable/sequence पर iteration |
| while | Condition True रहने तक repetition |
| range() | Numbers की range generate करने के लिए |
| break | Loop terminate करता है |
| continue | Current iteration skip करता है |
| Nested Loop | एक loop के अंदर दूसरा loop |
| Loop else | Normal loop completion पर execute हो सकता है |
RBSE Class 12 Important Questions
- Loop क्या है?
- Python में loops की आवश्यकता क्यों होती है?
- Python में loops के प्रकार लिखिए।
- for loop का syntax लिखिए।
- while loop का syntax लिखिए।
- range() function क्या है?
- range(1, 10) का क्या अर्थ है?
- for और while loop में अंतर लिखिए।
- Nested loop क्या है?
- break statement क्या है?
- continue statement क्या है?
- break और continue में अंतर लिखिए।
- 1 से 10 तक numbers print करने का program लिखिए।
- किसी number की multiplication table print करने का program लिखिए।
- Factorial निकालने का Python program लिखिए।
- Even numbers print करने का program लिखिए।
- List के elements का sum निकालने का program लिखिए।
Frequently Asked Questions (FAQs)
Python में Loop क्या है?
Loop ऐसा control structure है जिसका उपयोग statements को बार-बार execute करने के लिए किया जाता है।
Python में कितने प्रकार के loops हैं?
Python में मुख्य रूप से for और while loops
का उपयोग किया जाता है।
Python में range() का क्या उपयोग है?
range() का उपयोग loops में numbers की range पर iteration करने के लिए बहुत सामान्य रूप से किया जाता है।
break और continue में क्या अंतर है?
break loop को terminate करता है जबकि continue current iteration को skip करके अगली iteration पर चला जाता है।
Nested Loop क्या है?
एक loop के अंदर दूसरे loop का उपयोग करना Nested Loop कहलाता है।
अब अगला Topic पढ़ें
Python loops और control flow की विस्तृत जानकारी के लिए Python की official documentation देखें।