Python Tutorial - Loops - For u0026 While

Unlock Premium Features: AI explanations (Hinglish), Indian voice & Videos
Sign Up Free

๐Ÿ”„ Loops - Kaam Ko Repeat Karo

๐ŸŒŸ Real Life Mein:

Roz hum bahut kaam baar-baar karte hain:

๐Ÿชฅ "Subah 2 minute tak brush karo" - Time tak repeat
๐Ÿ“š "Har subject ki 5-5 exercises karo" - Har ek ke liye repeat
๐Ÿƒ "Jab tak thak na jao, daudho" - Condition tak repeat

Python mein loops se repetitive kaam automatically ho jata hai!

๐Ÿ” For Loop - Fixed Number of Times

Use case: Jab pata ho kitni baar repeat karna hai

Basic For Loop
# 1 se 5 tak print karo
for i in range(1, 6):
    print("Number:", i)

# Output:
# Number: 1
# Number: 2
# Number: 3
# Number: 4
# Number: 5

๐Ÿ“ Range Function Samjho

CodeMeaningOutput
range(5)0 se 4 tak0, 1, 2, 3, 4
range(1, 6)1 se 5 tak1, 2, 3, 4, 5
range(0, 10, 2)0 se 9, step 20, 2, 4, 6, 8
range(10, 0, -1)10 se 1, ulta10, 9, 8...1
Real Example - Table Print Karo
# 5 ka table
for i in range(1, 11):
    result = 5 * i
    print(f"5 x {i} = {result}")

โฐ While Loop - Condition Tak Chalo

Use case: Jab condition True hai tab tak repeat karo

While Loop Example
count = 1

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

# Output: 1, 2, 3, 4, 5

โš ๏ธ Infinite Loop Se Bacho!

Hamesha condition ko False karne ka raasta do!

# GALAT - Kabhi nahi rukega!
while True:
    print("Infinite loop!")  # Ctrl+C se stop karo

# SAHI - Condition change hoti hai
count = 0
while count < 5:
    print(count)
    count += 1  # Condition change!

๐ŸŽฎ Real-Life Example - Game Score

Practical Example
# Game - Jab tak lives hain, khelo
lives = 3
score = 0

while lives > 0:
    print(f"Lives: {lives}, Score: {score}")
    score += 10
    lives -= 1
    
print("Game Over!")
print(f"Final Score: {score}")

๐Ÿ”š Break aur Continue

Loop Control Karo:

  • break - Loop se bahar niklo
  • continue - Current iteration skip karo
Break Example
# Number mil gaya toh stop
for num in range(1, 11):
    if num == 5:
        print("Found 5!")
        break  # Loop se bahar
    print(num)

# Output: 1, 2, 3, 4, Found 5!
Continue Example
# Even numbers skip karo
for num in range(1, 6):
    if num % 2 == 0:
        continue  # Skip even
    print(num)

# Output: 1, 3, 5 (only odd)
Quick Tip: For loop tab use karo jab exact count pata ho. While loop tab use karo jab condition par depend kare!

๐Ÿค– AI Tutor Unlock Karo!

Apni language mein coding seekho - Hindi, Marathi, Gujarati aur 10+ Indian languages mein!

  • Hinglish mein explanations
  • Real-life examples
  • Beginner-friendly
Free Signup Karo

Example

for i in range(3):
    print("Hi")