Python Loops - Complete Detailed Guide

Loops Kya Hote Hain?

Loops programming me ek aise tool hote hain jinke through hum koi code block baar-baar execute kar sakte hain bina baar-baar uska code likhe. Repetitive work ko simplify karne ke liye loops bahut important hain. For example, agar hume list me sabhi items print karne hain, toh har item ke liye alag se print statement likhna inefficient hoga. Isliye loops ka use karte hain.

1. For Loop in Python

For loop ka use tab hota hai jab hume pata ho ki hum kis sequence (jaise list, tuple, string) me iteration kar rahe hain. For loop sequence ke ek-ek item ko lekar us par kaam karta hai.

Syntax:

for item in sequence:
    # yahan item pe actions
    

Example 1: Fruits print karna

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)
    

Output:

apple
banana
cherry

Example 2: Range function ke sath loop

Range se hum numbers ka series generate kar ke usme loop chala sakte hain.

for i in range(1, 6):
    print("Number:", i)
    

Output: 1, 2, 3, 4, 5 (ek ek karke print karega)

Example 3: String ke characters par for loop

word = "Python"
for letter in word:
    print(letter)
    

Output:

P
y
t
h
o
n

2. While Loop in Python

While loop tab use hota hai jab hume koi kaam tab tak repeat karna ho jab tak koi condition true ho. Unlike for loop, jab tak condition true hai loop chalata rahega, warna ruk jaata hai.

Syntax:

while condition:
    # loop ka code block
    

Example 1: 1 se 5 tak counting

count = 1
while count <= 5:
    print("Count:", count)
    count += 1
    

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Example 2: User input se loop

command = ""
while command.lower() != "quit":
    command = input("Kuch likho (quit likho rokne ke liye): ")
    print(f"Apne likha: {command}")
    

Yeh loop tab tak chalega jab tak user "quit" na likhe.

Infinite Loop ka Khatra

Agar condition kabhi false na ho to loop infinite chalta rahega. Yeh avoid karne ke liye loop ke andar state-update ya break condition zaroori hai.

while True:
    print("Yeh infinite loop hai! (Break lagao)");
    break  # loop ko rokne ke liye break use karna chahiye
    

3. Loop Control Statements

Loop ke andar kuch statements hoti hain jo loop ke flow ko control karte hain:

Break Statement

Loop ko turant rokne ke liye break use hota hai:

for i in range(1, 10):
    if i == 4:
        break
    print(i)
    

Output: 1 2 3 (4 par loop ruk jaata hai)

Continue Statement

Current iteration skip karne ke liye continue use hota hai:

for i in range(1, 6):
    if i == 3:
        continue
    print(i)
    

Output: 1 2 4 5 (3 skip ho gaya)

Pass Statement

Pass statement koi action nahi karta, bas placeholder ke liye use hota hai jab code likhna baad me ho:

for i in range(1, 4):
    if i == 2:
        pass  # is jagah future me code likhenge
    else:
        print(i)
    

Output: 1 3

4. Nested Loops

Ek loop ke andar doosra loop likhna nested loop hota hai. Yeh 2D data jaise matrix ya table process karne ke liye useful hota hai.

for i in range(1, 4):
    for j in range(1, 3):
        print(f"i={i}, j={j}")
    

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

5. Practical Example

1 se 10 tak numbers print karo, aur 5 se divisible number ke liye special message do:

for num in range(1, 11):
    if num % 5 == 0:
        print(num, "- Divisible by 5")
    else:
        print(num)
    

Conclusion

Loops programming me repetitive tasks ko efficient banate hain. For loops sequences aur collections ke liye best hain, jabki while loops complex conditions ke liye useful hote hain. Break aur continue se hum loop ke flow ko customize kar sakte hain. Nested loops se complex multi-dimensional problems solve hoti hain.

Practice se hi loops par mastery aati hai, isliye chhote-chhote programs bana kar loops ka daily use karte rahna chahiye.