🔁 Python में Loop क्या होता है?
Table of Contents
Toggleजब हमें एक ही कोड को बार-बार चलाना होता है, तो बार-बार copy-paste करने की बजाय हम Loop का इस्तेमाल करते हैं। Python में loop एक powerful तरीका है जिससे हम repeatable tasks को आसान और short कर सकते हैं।
✅ Task Repetition
एक ही statement को कई बार चलाना है? Loop से आसान हो जाता है।
🔄 Dynamic Data Processing
List या Dictionary में सभी elements को process करना हो? Loop best है!
⏱️ Time-Saving Automation
100 files का नाम बदलना हो? Loop से सेकंड्स में काम हो सकता है।

ऊपर दिए गए Diagram में Python के Loop Structure को simplified form में दिखाया गया है।
🤔 Loop की ज़रूरत क्यों पड़ती है?
जब हमें एक जैसा code कई बार चलाना होता है, तो उसे बार-बार लिखने से न सिर्फ समय बर्बाद होता है, बल्कि गलती की संभावना भी बढ़ जाती है। Loop हमें देता है:
⏳ समय की बचत
Same काम को कई बार लिखने की ज़रूरत नहीं, एक बार loop लगाओ और चलाओ।
🚫 Code Duplication से बचाव
Code clean और readable बनता है जब हम loop का use करते हैं।
📊 Data पर काम आसान
List, Tuple या Files पर बार-बार operation loop से बहुत आसान हो जाता है।
🔁 For Loop क्या है? – सिंटैक्स और उदाहरण
Python में for loop का इस्तेमाल किसी sequence (जैसे list, string, range) के हर element पर एक-एक करके काम करने के लिए किया जाता है।
📌 For Loop का Basic Syntax:
for item in sequence: # Do something with item
🎒 Example: एक Backpack में रखी Items को Print करना
backpack = ["book", "pen", "notebook", "bottle"] for item in backpack: print(item)
Output: book
, pen
, notebook
, bottle
🔄 While Loop क्या है? – सिंटैक्स और उदाहरण
Python में while loop तब तक चलता रहता है जब तक उसकी दी गई condition true
📌 While Loop का Basic Syntax:
while condition: # Do something
🔢 Example: एक Counter को 1 से 5 तक Print करना
i = 1 while i <= 5: print(i) i += 1
Output: 1 2 3 4 5
♾️ Infinite Loop क्या होता है Python में?
जब कोई loop ऐसी condition में चला दिया जाए जो कभी false नहीं होती, तो वो loop कभी बंद नहीं होता। इसे Infinite Loop कहा जाता है। यह system hang करने का कारण बन सकता है अगर सही से control न किया जाए।
⚠️ Warning:
Infinite loop आपके code को कभी न रुकने वाली स्थिति में डाल सकता है। इसे रोकने के लिए Ctrl+C का प्रयोग करें (या Jupyter में Kernel Restart करें)।
🔁 Example: एक Simple Infinite Loop
while True: print("This loop will never stop!")
Output: यह message infinite बार print होता रहेगा जब तक manually बंद न किया जाए।
🧭 Loop Control Statements – Break, Continue और Pass
Python में कुछ ऐसे keywords होते हैं जो loop के behavior को control करते हैं। ये statements loop को बीच में रोक सकते हैं, skip कर सकते हैं या temporarily खाली छोड़ सकते हैं।
⛔ break
Loop को तुरंत रोकने के लिए break का इस्तेमाल होता है।
for i in range(1, 6): if i == 3: break print(i)
Output: 1 2
🔁 continue
Current iteration को skip करने के लिए continue का इस्तेमाल किया जाता है।
for i in range(1, 6): if i == 3: continue print(i)
Output: 1 2 4 5
🚧 pass
कोई action नहीं करना है, लेकिन syntax valid रखना है तो pass use करें।
for i in range(1, 4): if i == 2: pass print(i)
Output: 1 2 3
🔂 Nested Loop क्या होता है Python में?
जब एक loop के अंदर दूसरा loopNested Loop
🧩 Nested Loop का Syntax:
for i in range(1, 4): for j in range(1, 3): print(i, j)
📌 Output:
1 1 1 2 2 1 2 2 3 1 3 2
🔗 Python में Loop और Function का Combination कैसे Use होता है?
Python में हम Function के अंदर looploop के अंदर function
🧪 Function के अंदर Loop का Use
def print_numbers(): for i in range(1, 6): print(i) print_numbers()
Output: 1 2 3 4 5
🔁 Loop के अंदर Function Call
def greet(name): print("Hello", name) names = ["Aman", "Riya", "Vikas"] for person in names: greet(person)
Output: Hello Aman, Hello Riya, Hello Vikas
🔁 Python Loop Examples (For & While)
1️⃣ For Loop से 1 से 5 तक Print करें
for i in range(1, 6):
print(i)
Output: 1 2 3 4 5
2️⃣ While Loop से Countdown
count = 5
while count > 0:
print(count)
count -= 1
Output: 5 4 3 2 1
3️⃣ List के Items को Print करें
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
Output: apple banana mango
4️⃣ Even Numbers Print करें
for i in range(2, 11, 2):
print(i)
Output: 2 4 6 8 10
5️⃣ Odd Numbers While Loop से
i = 1
while i <= 9:
print(i)
i += 2
Output: 1 3 5 7 9
6️⃣ String के हर Letter को Print करें
word = "Vista"
for letter in word:
print(letter)
Output: V i s t a
7️⃣ Loop + Break Statement
for i in range(1, 10):
if i == 5:
break
print(i)
Output: 1 2 3 4
8️⃣ Loop + Continue Statement
for i in range(1, 6):
if i == 3:
continue
print(i)
Output: 1 2 4 5
9️⃣ Nested Loop से Pair बनाएं
for i in range(1, 3):
for j in range(1, 3):
print(i, j)
Output: 1 1, 1 2, 2 1, 2 2
🔟 Loop के साथ Function Call
def greet(name):
print("Hello", name)
names = ["Ankit", "Riya"]
for n in names:
greet(n)
Output: Hello Ankit, Hello Riya
🎯 Interview में पूछे जाने वाले Python Loop के महत्वपूर्ण सवाल
Interviews में loops को लेकर अक्सर conceptual और practical दोनों तरह के सवाल पूछे जाते हैं। नीचे कुछ commonly पूछे जाने वाले questions दिए गए हैं:
1️⃣ For और While Loop में क्या अंतर है?
For loop का use तब होता है जब iterations की संख्या predefined हो। While loop तब use होता है जब condition-based looping करनी हो।
2️⃣ Infinite Loop क्या होता है?
ऐसा loop जो कभी बंद नहीं होता और लगातार चलता रहता है जब तक कि उसे manually न रोका जाए।
3️⃣ Loop Control Statements कौन-कौन से हैं?
Python में break, continue और pass control statements हैं जो loop के flow को modify करते हैं।
4️⃣ Nested Loop क्या है?
जब एक loop के अंदर दूसरा loop होता है, उसे nested loop कहते हैं। इसका use tabular data या patterns में किया जाता है।
5️⃣ While loop का एक real-life example बताइए
जब user से input लेते रहना हो जब तक वह “exit” न लिखे — यह while loop का एक practical example है।
🧠 अब आपको Python Loops अच्छे से समझ आ गए होंगे!
For Loop, While Loop, Nested Loops और Control Statements को समझना Python सीखने की foundation है। अगर आप इन concepts पर practice करते हैं, तो आप किसी भी project या interview के लिए ready हो सकते हैं।