Python While Loop Tutorial

What is a While Loop and When to Use It?

A while loop in Python repeatedly executes a block of code as long as a given condition remains True. It is useful when the number of iterations is not known in advance, and you want the loop to run until a specific condition changes.

Python में while loop तब तक कोड को बार-बार चलाता है जब तक दिया गया condition True रहता है। इसका उपयोग तब होता है जब आपको पहले से पता नहीं होता कि loop कितनी बार चलेगा, और आप इसे तब तक चलाना चाहते हैं जब तक कोई specific condition बदल जाए।

Difference Between For Loop and While Loop

  • For loop is used when the number of iterations is known or finite.
  • While loop is used when the number of iterations is not known and depends on a condition.
  • For loops iterate over sequences like lists, tuples, strings.
  • While loops iterate based on a condition and may run indefinitely if the condition never becomes False.
  • For loops are generally more compact and easier for fixed iteration.
  • While loops provide more flexibility for unknown iteration counts.
  • For loop तब उपयोग होता है जब iteration की संख्या ज्ञात या सीमित हो।
  • While loop तब उपयोग होता है जब iteration की संख्या ज्ञात न हो और condition पर निर्भर हो।
  • For loop आमतौर पर सूची, टपल, स्ट्रिंग जैसी sequences पर चलता है।
  • While loop condition के आधार पर चलता है और अनंत loop भी बना सकता है यदि condition कभी False न हो।
  • For loop fixed iteration के लिए अधिक compact और आसान होता है।
  • While loop अधिक लचीला होता है जब iteration की संख्या अज्ञात हो।

10 Examples of Python While Loop with Explanations

Example 1: Simple While Loop
i = 1
while i <= 5:
    print(i)
    i += 1

Print numbers from 1 to 5 using a while loop that increments i until the condition fails.

1 से 5 तक नंबर print करें, while loop i को बढ़ाता है जब तक condition false न हो जाए।

Output:

1
2
3
4
5
Example 2: While Loop with Break
i = 1
while True:
    print(i)
    if i == 3:
        break
    i += 1

Infinite while loop that breaks when i equals 3.

असीमित while loop जो i == 3 होने पर break कर देता है।

Output:

1
2
3
Example 3: While Loop with Continue
i = 0
while i < 5:
    i += 1
    if i == 3:
        continue
    print(i)

Skips printing number 3 using continue inside while loop.

while loop में continue का उपयोग कर नंबर 3 को skip करें।

Output:

1
2
4
5
Example 4: Using Else with While Loop
i = 1
while i <= 3:
    print(i)
    i += 1
else:
    print('Loop ended')

Else block runs after while loop finishes normally.

while loop के सफल समाप्ति के बाद else block चलता है।

Output:

1
2
3
Loop ended
Example 5: While Loop to Sum Numbers
total = 0
num = 1
while num <= 5:
    total += num
    num += 1
print(total)

Calculate sum of numbers from 1 to 5 using a while loop.

while loop का उपयोग कर 1 से 5 तक के नंबरों का योग करें।

Output:

15
Example 6: Nested While Loop
i = 1
while i <= 3:
    j = 1
    while j <= 2:
        print(f'i={i}, j={j}')
        j += 1
    i += 1

Nested while loops print combinations of i and j values.

Nested while loops i और j के combinations print करते हैं।

Output:

i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2
i=3, j=1
i=3, j=2
Example 7: While Loop User Input
command = ''
while command.lower() != 'exit':
    command = input('Enter command: ')
print('Exited loop')

Loop continues asking for user input until user types 'exit'.

जब तक user 'exit' न लिखे, तब तक input मांगता रहे।

Output:

Enter command: hi
Enter command: exit
Exited loop
Example 8: While Loop with Flag
flag = True
count = 0
while flag:
    count += 1
    if count == 4:
        flag = False
    print(count)

Using a flag variable to control the while loop execution.

flag variable का उपयोग कर while loop को control करें।

Output:

1
2
3
4
Example 9: While Loop with Decrement
n = 5
while n > 0:
    print(n)
    n -= 1

Counting down from 5 to 1 using a while loop with decrement.

while loop में decrement कर 5 से 1 तक गिनती करें।

Output:

5
4
3
2
1
Example 10: Infinite Loop with Break
while True:
    response = input('Type stop to exit: ')
    if response == 'stop':
        break
print('Loop stopped')

Infinite loop asking user input and breaks when user types 'stop'.

असीमित loop user से input लेता है और 'stop' पर बंद होता है।

Output:

Type stop to exit: hello
Type stop to exit: stop
Loop stopped