Python Loops (Part 7.1 – Section D)
Python while Loop, Infinite Loop, break, continue और pass | Complete Guide with Real-Life Examples
यह भाग पिछले Section C3 का अगला भाग है। अब हम Python के दूसरे सबसे महत्वपूर्ण Loop
whileको विस्तार से सीखेंगे।
Introduction
अब तक आपने for Loop सीखा।
लेकिन Programming में हमेशा पहले से यह पता नहीं होता कि Loop कितनी बार चलेगा।
उदाहरण के लिए—
ATM कितनी बार PIN Enter करने देगा?
Login कितनी बार Try किया जाएगा?
YouTube कब तक अगली Videos Load करेगा?
WhatsApp कब तक Messages Receive करेगा?
इन सभी Programs में पहले से Loop की संख्या तय नहीं होती।
यहीं पर while Loop का उपयोग किया जाता है।
while Loop क्या होता है?
while Loop तब तक चलता रहता है जब तक उसकी Condition True रहती है।
जैसे ही Condition False होती है—
Loop समाप्त हो जाता है।
सरल शब्दों में—
जब तक शर्त सही है, तब तक Code बार-बार चलाओ।
Syntax
while condition:
statement
पहला while Program
count = 1
while count <= 5:
print(count)
count += 1
Output
1
2
3
4
5
यह Program कैसे काम करता है?
शुरुआत
count = 1
Condition
1 <= 5
सही है।
इसलिए
1
अब
count = 2
फिर Condition
2 <= 5
फिर Print
इसी प्रकार
5 तक चलता है।
जब
count = 6
हो जाता है।
Condition False हो जाती है।
Loop समाप्त।
Flow Diagram
Start
↓
Condition Check
↓
True
↓
Run Code
↓
Update Variable
↓
Condition Check Again
↓
False
↓
Stop
for और while में अंतर
| for Loop | while Loop |
|---|---|
| Iterations पहले से ज्ञात हों | Iterations पहले से ज्ञात न हों |
| Sequence पर चलता है | Condition पर चलता है |
| range() अधिक उपयोग होती है | Variable Update आवश्यक होता है |
Example – 1 से 10 तक Print
number = 1
while number <= 10:
print(number)
number += 1
Reverse Counting
number = 10
while number >= 1:
print(number)
number -= 1
Output
10
9
8
7
6
5
4
3
2
1
User Input Example
number = int(input("Enter Number : "))
count = 1
while count <= number:
print(count)
count += 1
यदि User
20
लिखे—
तो Program
1
से
20
तक Print करेगा।
Infinite Loop क्या होता है?
यदि Condition कभी False ही न हो—
तो Loop हमेशा चलता रहेगा।
इसे Infinite Loop कहते हैं।
उदाहरण
while True:
print("Python")
यह Program कभी नहीं रुकेगा।
इसे रोकने के लिए
Ctrl + C
दबाना पड़ता है।
Infinite Loop कहाँ उपयोग होता है?
हर जगह नहीं।
लेकिन कई महत्वपूर्ण जगहों पर—
Game Loop
Chat Server
ATM Software
CCTV Monitoring
Live Sensor Reading
Web Server
Robot Controller
इन सभी में Program लगातार चलता रहता है।
break Statement
break का अर्थ है—
Loop को तुरंत समाप्त कर दो।
Example
for i in range(1,11):
if i == 6:
break
print(i)
Output
1
2
3
4
5
6 आते ही Loop समाप्त।
Real Life Example
ATM में PIN तीन बार गलत हो जाए—
तो क्या होता है?
Machine आगे PIN पूछती रहती है?
नहीं।
वह Transaction रोक देती है।
यही
break
का वास्तविक उपयोग है।
ATM PIN Program
correct_pin = "1234"
attempt = 1
while attempt <= 3:
pin = input("Enter PIN : ")
if pin == correct_pin:
print("Login Successful")
break
else:
print("Wrong PIN")
attempt += 1
else:
print("Card Blocked")
continue Statement
continue का अर्थ है—
बाकी Code छोड़ो
और अगली Iteration पर जाओ।
Example
for i in range(1,11):
if i == 5:
continue
print(i)
Output
1
2
3
4
6
7
8
9
10
ध्यान दें—
5 Print नहीं हुआ।
continue कहाँ उपयोग होता है?
मान लीजिए—
100 Students हैं।
Absent Students को Skip करना है।
बाकी सभी का Result बनाना है।
यही
continue
का उपयोग है।
pass Statement
कई बार Program अभी पूरा नहीं होता।
लेकिन Syntax Error भी नहीं चाहिए।
तब
pass
लिखते हैं।
Example
for i in range(5):
pass
यह Program चलेगा।
लेकिन कोई Output नहीं देगा।
Login System Project
username = "admin"
password = "python123"
while True:
user = input("Username : ")
pwd = input("Password : ")
if user == username and pwd == password:
print("Login Successful")
break
print("Try Again")
Mini Project
Guess the Number
secret = 7
while True:
guess = int(input("Guess : "))
if guess == secret:
print("Correct")
break
else:
print("Try Again")
Common Mistakes
गलती 1
Variable Update भूल जाना।
count = 1
while count <= 5:
print(count)
यह Infinite Loop बन जाएगा।
गलती 2
Condition हमेशा True रखना।
गलती 3
गलत स्थान पर
break
लिख देना।
Best Practices
हमेशा Exit Condition रखें।
Infinite Loop केवल आवश्यकता होने पर ही उपयोग करें।
breakऔरcontinueका उपयोग सोच-समझकर करें।Variable Update कभी न भूलें।
Interview Questions
while Loop क्या है?
for और while में अंतर?
Infinite Loop क्या होता है?
break और continue में अंतर?
pass का उपयोग कब करते हैं?
while True क्यों लिखा जाता है?
Practice Assignment
Beginner
✅ 1–100 Print
✅ Reverse Counting
✅ Even Numbers
✅ Odd Numbers
Intermediate
✅ Sum of Numbers
✅ Factorial using while
✅ Prime using while
✅ Fibonacci using while
Advanced
✅ ATM Simulation
✅ Login System
✅ Guess Number Game
✅ Password Validation
✅ Menu Driven Calculator
Chapter Summary
इस अध्याय में आपने सीखा—
while Loop
Infinite Loop
break
continue
pass
Login Logic
ATM Logic
Guess Number Project
Best Practices
Interview Questions
अब आप Python के दोनों प्रमुख Loops (for और while) को समझ चुके हैं। यह ज्ञान आगे Data Structures, File Handling, Automation और Machine Learning तक आपके काम आएगा।
अगले भाग (Part 7.1 – Section E)
अब हम इस पूरे Chapter का सबसे शानदार भाग शुरू करेंगे—
🚀 Real World Automation Programs
🏧 ATM Simulation Project
🏫 School Management Project
🧾 Billing System
📊 Student Result Management
🎮 Mini Games
💼 30+ Practice Programs
🎯 Google, Microsoft, Amazon Interview Questions
📚 Complete Chapter Revision
इसके बाद आपका Python Loops Chapter पूरी तरह Complete हो जाएगा और Beginner से Intermediate Level तक की मजबूत पकड़ बन जाएगी।
0 Comments
कमेंट केवल पोस्ट से रिलेटेड करें