Python While Loop in Hindi – Syntax, Examples & Loop में User Input
Python While Loop in Hindi – Syntax, Examples, Flowchart और Practice Programs
तो loops का topic आते ही थोड़ा confusion हो जाता है — क्योंकि loops बहुत बार use होते हैं, लेकिन शुरुआत में इनकी working समझना थोड़ा tricky लगता है। अगर आपने अभी तक हमारा Python Tutorial in Hindi नहीं पढ़ा है, तो पहले वहाँ से basics clear कर लें, फिर यह post और भी आसानी से समझ आएगा।
Python While Loop एक ऐसा looping statement है, जिसका इस्तेमाल किसी code block को तब तक बार-बार execute करने के लिए किया जाता है, जब तक दी गई condition True रहती है। अगर आपको पहले से पता नहीं है कि loop कितनी बार चलेगा, या loop किसी condition पर depend करता है, तो while loop ही सही choice होता है।
Python While Loop क्या है?
The while loop in Python is used to execute a block of code as long as a specified condition remains True. It checks the condition before each iteration and continues to execute the code block until the condition evaluates to False.
Examplecount = 1
while count <= 5: print(count) count += 1
यहाँ condition count <= 5 को प्रत्येक iteration से पहले evaluate किया जाता है। अगर condition True होती है, तो code block execute होता है। अगर condition False हो जाती है, तो loop terminate हो जाता है और program loop के बाद वाले next statement के साथ continue करता है।
While Loop का Syntax
while condition:
# code to be executed
यहाँ:
whilePython का keyword है। अगर आप Python के सारे keywords के बारे में detail में जानना चाहते हैं, तो Python Keywords in Hindi वाला guide जरूर देखें।conditionवह expression है, जिसे Python हर बार check करता है।- condition
Trueहोने पर loop का code execute होता है। - condition
Falseहोने पर loop समाप्त हो जाता है। - Python में indentation बहुत important है — बिना सही indentation के program error देगा।
While Loop कैसे काम करता है?
While loop की working को इन steps से समझ सकते हैं:
- सबसे पहले condition check होती है।
- अगर condition
Trueहै, तो loop के अंदर का code execute होता है। - फिर condition दोबारा check होती है।
- यह process तब तक repeat होता है, जब तक condition
Trueरहती है। - जैसे ही condition
Falseहोती है, loop terminate हो जाता है।
एक example से देखते हैं:
count = 1
while count <= 3:
print(count)
count += 1
इसमें step by step क्या हो रहा है:
- पहले
count = 1set होता है। 1 <= 3→True, तो1print होगा।countबढ़कर2हो जाएगा।2 <= 3→True, तो2print होगा।countबढ़कर3हो जाएगा।3 <= 3→True, तो3print होगा।countबढ़कर4हो जाएगा।4 <= 3→False, इसलिए loop यहीं रुक जाएगा।
याद रखने वाली बात: हर iteration के बाद condition को दोबारा check करना ही while loop का core logic है।
Python While Loop का Simple Example
number = 1
while number <= 5:
print("Python")
number += 1
Output:
Python
Python
Python
Python
Python
यहाँ "Python" पाँच बार print होगा, क्योंकि condition पाँच iterations तक True रहती है।
While Loop में Counter का Use
While loop में counter variable का इस्तेमाल बहुत common है, क्योंकि इसी से हम decide करते हैं कि loop कब तक चलेगा।
count = 1
while count <= 10:
print(count)
count += 1
Output:
1
2
3
4
5
6
7
8
9
10
यहाँ count += 1 हर iteration के बाद counter की value को एक से बढ़ाता है।
While Loop से Reverse Counting
While loop का इस्तेमाल reverse counting के लिए भी किया जा सकता है।
count = 5
while count >= 1:
print(count)
count -= 1
Output:
5
4
3
2
1
यहाँ count -= 1 हर iteration के बाद value को एक से कम करता है।
Python While Loop में User Input
While loop का इस्तेमाल user से input लेकर भी किया जा सकता है, जिससे program interactive बन जाता है।
number = int(input("Enter a number: "))
while number <= 5:
print(number)
number += 1
इस program में user द्वारा दिए गए number से counting शुरू होगी और 5 तक चलेगी।
While Loop से 1 से 10 तक Sum
While loop का इस्तेमाल numbers का sum निकालने के लिए भी किया जा सकता है।
number = 1
total = 0
while number <= 10:
total += number
number += 1
print("Total:", total)
Output:
Total: 55
इस program में 1 से 10 तक सभी numbers को जोड़कर total variable में store किया जाता है।
While Loop में Even Numbers
While loop से 1 से 10 तक even numbers print कर सकते हैं।
number = 1
while number <= 10:
if number % 2 == 0:
print(number)
number += 1
Output:
2
4
6
8
10
यहाँ % operator का इस्तेमाल यह check करने के लिए किया गया है कि number 2 से completely divisible है या नहीं।
While Loop में Odd Numbers
इसी तरह odd numbers भी print किए जा सकते हैं:
number = 1
while number <= 10:
if number % 2 != 0:
print(number)
number += 1
Output:
1
3
5
7
9
While Loop With Break
break statement का इस्तेमाल loop को तुरंत terminate करने के लिए किया जाता है — चाहे condition अभी भी True क्यों न हो।
count = 1
while count <= 10:
if count == 6:
break
print(count)
count += 1
Output:
1
2
3
4
5
जब count == 6 होता है, तब break loop को तुरंत खत्म कर देता है।
While Loop With Continue
continue statement current iteration को skip करके सीधा अगली iteration पर चला जाता है।
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
Output:
1
2
4
5
इस example में 3 को skip कर दिया जाता है, और बाकी numbers normally print होते हैं।
Infinite While Loop
अगर while loop की condition हमेशा True रहती है, तो loop कभी खत्म नहीं होगा। इसे Infinite Loop कहते हैं।
while True:
print("Hello Python")
यह program लगातार "Hello Python" print करता रहेगा, जब तक आप manually program stop न करें।
Infinite loop को control में रखने के लिए break का इस्तेमाल किया जा सकता है:
while True:
number = int(input("Enter 0 to stop: "))
if number == 0:
break
print("You entered:", number)
यह program तब तक चलता रहेगा जब तक user 0 enter नहीं करता।
While Loop में Infinite Loop से कैसे बचें?
While loop लिखते समय यह सुनिश्चित करें कि condition किसी point पर False हो जाए, वरना program hang हो सकता है।
गलत example:
count = 1
while count <= 5:
print(count)
इस program में count की value कभी change नहीं होगी। इसलिए condition हमेशा True रहेगी और loop infinite बन जाएगा।
सही तरीका:
count = 1
while count <= 5:
print(count)
count += 1
यहाँ count लगातार बढ़ता है, इसलिए एक point आने पर condition False हो जाती है और loop सही तरह खत्म होता है।
While Loop और For Loop में अंतर
Python में while और for दोनों ही loops हैं, लेकिन इनके इस्तेमाल का तरीका अलग होता है।
| While Loop | For Loop |
|---|---|
| Condition के आधार पर चलता है | Sequence या iterable पर iterate करता है |
| Iterations की संख्या पहले से fix होना जरूरी नहीं | जब sequence की length पता हो तब convenient है |
| User input और condition-based programs में useful | List, tuple, string आदि पर iteration के लिए useful |
| Condition manually manage करनी पड़ सकती है | Iteration automatically manage होता है |
While Loop:
count = 1
while count <= 5:
print(count)
count += 1
For Loop:
for count in range(1, 6):
print(count)
दोनों programs 1 से 5 तक numbers print करेंगे, बस approach अलग है।
While Loop में Indentation
Python में indentation बहुत important है, और while loop भी इससे exempt नहीं है।
सही:
count = 1
while count <= 5:
print(count)
count += 1
गलत:
count = 1
while count <= 5:
print(count)
इसलिए while loop के अंदर लिखे statements को proper indentation देना जरूरी है, वरना Python IndentationError दे देगा।
Practical Example: Password Attempts
While loop का इस्तेमाल limited attempts वाले programs में काफी किया जाता है — जैसे login systems।
password = ""
attempts = 0
while password != "python123" and attempts < 3:
password = input("Enter password: ")
attempts += 1
if password == "python123":
print("Login successful")
else:
print("Too many attempts")
यह example दिखाता है कि condition-based situations में while loop कितना useful हो सकता है।
Practical Example: Number Guessing
While loop का इस्तेमाल एक simple number guessing program बनाने में भी किया जा सकता है।
secret_number = 7
guess = 0
while guess != secret_number:
guess = int(input("Guess the number: "))
print("Correct! You guessed the number.")
जब तक user सही number enter नहीं करता, loop चलता रहेगा।
While Loop के Important Points
whileloop एक condition-based loop है।- condition
Trueहोने पर loop execute होता है। - condition
Falseहोने पर loop समाप्त हो जाता है। - loop variable को update करना जरूरी हो सकता है, वरना infinite loop बन सकता है।
breakloop को तुरंत terminate करता है।continuecurrent iteration को skip करता है।- गलत condition से infinite loop बन सकता है।
- Python में indentation का सही होना जरूरी है।
अगर आप loops के साथ-साथ databases भी सीखना चाहते हैं, तो MySQL Tutorial in Hindi और Database Introduction in Hindi वाले articles भी काफी useful रहेंगे।
FAQ
1. Python में While Loop क्या है?
Python में while loop एक looping statement है, जिसका इस्तेमाल किसी code block को तब तक execute करने के लिए किया जाता है, जब तक दी गई condition True रहती है।
2. Python While Loop का Syntax क्या है?
while condition:
# code
3. While Loop कब समाप्त होता है?
जब while loop की condition False हो जाती है, तब loop terminate हो जाता है।
4. Python While Loop में Break क्या करता है?
break statement loop को तुरंत terminate कर देता है, चाहे condition अभी True ही क्यों न हो।
5. While Loop में Continue क्या करता है?
continue current iteration को skip करके loop की अगली iteration शुरू करता है।
6. Python में Infinite Loop क्या है?
जब loop की condition हमेशा True रहती है और loop समाप्त नहीं होता, तो उसे infinite loop कहते हैं।
while True:
print("Python")
7. While Loop में Counter क्यों इस्तेमाल किया जाता है?
Counter का इस्तेमाल loop की iterations को control करने और condition को eventually False बनाने के लिए किया जाता है।
8. While Loop और For Loop में क्या अंतर है?
while loop मुख्य रूप से condition के आधार पर चलता है, जबकि for loop किसी sequence या iterable के elements पर iterate करने के लिए commonly इस्तेमाल होता है।
9. क्या While Loop के अंदर If Statement का इस्तेमाल कर सकते हैं?
हाँ। while loop के अंदर if, elif और else statements का इस्तेमाल किया जा सकता है।
10. क्या Python While Loop में User Input लिया जा सकता है?
हाँ। User input के आधार पर condition check करके while loop का इस्तेमाल interactive programs बनाने में किया जा सकता है।
Conclusion
Python का While Loop condition-based programming के लिए एक बहुत ही useful looping statement है। इसकी मदद से हम किसी code को तब तक execute कर सकते हैं, जब तक कोई condition True रहती है।
अगर आप Python सीखना शुरू कर रहे हैं, तो while, for, break और continue को अच्छे से समझना जरूरी है, क्योंकि real-world Python programs में इनका बहुत ज्यादा इस्तेमाल होता है। अगले step में आप Python For Loop, range() function और nested loops सीख सकते हैं।
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें