Chapter 8: Loops
Loops
This is like going from cooking one plate of biryani by hand → teaching your robot chef to make 100 plates automatically without you repeating the same steps 100 times.
After variables, if-statements, and lists, loops are where programming starts feeling magical — because you write code once and it runs many times automatically.
1. What is a Loop? (The super simple explanation)
A loop is a way to repeat a block of code multiple times — either:
- a known/fixed number of times (for loop — most common for beginners)
- or until a condition becomes false (while loop)
Without loops you’d have to copy-paste the same code 10 or 100 times — boring, error-prone, and bad style.
Real-life Hyderabad analogy everyone gets:
You want to tell your friend “Study Python daily!” for the next 7 days.
Without loop → you say it 7 times manually.
With loop → you say once: “Repeat this message 7 times: Study Python daily!”
Computer does the boring repetition for you — fast and perfect.
2. Two Main Types of Loops in Python (and almost every language)
| Type | When to use | Real-life example | Python keyword |
|---|---|---|---|
| for | You know how many times / looping over items | Print attendance for 11 players in team | for |
| while | Repeat until something happens / condition true | Keep asking for password until correct | while |
for is safer & more common for beginners — use it whenever possible.
3. The for Loop (Your new best friend)
Syntax pattern (memorize this shape):
|
0 1 2 3 4 5 6 7 8 |
for item in sequence: # colon : is MUST # indented block # 4 spaces # this runs once for each item |
Most common ways to use for:
3.1 Classic – using range() (counting numbers)
|
0 1 2 3 4 5 6 7 |
for i in range(5): # 0,1,2,3,4 → 5 times print("Study Python today! Day", i+1) |
Output:
|
0 1 2 3 4 5 6 7 8 9 |
Study Python today! Day 1 Study Python today! Day 2 ... Study Python today! Day 5 |
range(start, stop, step) — very flexible
|
0 1 2 3 4 5 6 7 8 9 10 11 |
for i in range(1, 11, 2): # 1,3,5,7,9 print(i) for i in range(10, 0, -1): # countdown 10 to 1 print("T minus", i, "...") print("Blast off! 🚀") |
3.2 Looping over a list (super powerful!)
|
0 1 2 3 4 5 6 7 8 9 |
foods = ["Biryani", "Haleem", "Irani Chai", "Pani Puri", "Osmania"] for food in foods: print("I love", food, "from Hyderabad! 🍛") |
Output:
|
0 1 2 3 4 5 6 7 8 |
I love Biryani from Hyderabad! 🍛 I love Haleem from Hyderabad! 🍛 ... |
3.3 With index (when you need position)
|
0 1 2 3 4 5 6 7 8 9 |
cricketers = ["Virat", "Rohit", "Bumrah"] for i in range(len(cricketers)): print(f"Player {i+1}: {cricketers[i]}") |
Or better (Pythonic way):
|
0 1 2 3 4 5 6 7 |
for idx, player in enumerate(cricketers, start=1): print(f"Player {idx}: {player}") |
4. The while Loop (Repeat while condition is True)
Syntax:
|
0 1 2 3 4 5 6 7 8 |
while condition: # runs as long as condition True # indented block # usually change something inside so it eventually stops! |
Example – Keep guessing password
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
password = "hyderabad123" guess = input("Enter password: ") while guess != password: print("Wrong! Try again.") guess = input("Enter password: ") print("Correct! Welcome boss 😎") |
Danger – Infinite loop (very common beginner mistake!)
|
0 1 2 3 4 5 6 7 8 9 |
count = 1 while count <= 5: print("Hello") # forgot count = count + 1 → loops forever! |
→ Computer hangs or you Ctrl+C to stop. Rule: In while loop → always make sure condition will become False eventually (usually by updating a variable inside).
5. Important Loop Controls (break, continue)
- break → jump out of loop immediately
- continue → skip this iteration, go to next
|
0 1 2 3 4 5 6 7 8 9 10 11 |
for i in range(1, 11): if i == 7: break # stop at 7 print(i) # Output: 1 2 3 4 5 6 |
|
0 1 2 3 4 5 6 7 8 9 10 11 |
for i in range(1, 11): if i % 2 == 0: # even numbers continue # skip printing print(i, "is odd") # Output: only odd numbers 1 3 5 7 9 |
6. Combining Loops + Lists + If (real power)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
temperatures = [38, 42, 35, 41, 29, 33] hot_days = 0 for temp in temperatures: if temp >= 40: print("Today was HOT:", temp, "°C 🥵") hot_days += 1 else: print("Normal day:", temp, "°C") print("Total hot days this week:", hot_days) |
7. Common Beginner Mistakes (from real students)
- Forgetting to update variable in while → infinite loop
- Off-by-one error with range() → range(1, 10) misses 10
- Indentation wrong → loop runs only once or error
- Modifying list while looping over it (advanced, but crashes sometimes)
- Using = instead of += for counters
- while True without break → infinite unless careful
8. Your 10–15 Minute Mini Challenge (do it today!)
Open replit.com or any Python editor and try one (or both):
Challenge A – for loop
- Make list of 7 favorite Hyderabad foods
- Loop and print “I want to eat [food] this weekend!” for each
Challenge B – while loop
- Start with balance = 5000
- While balance > 0:
- Ask “Withdraw how much? (0 to stop)”
- If 0 → break
- If > balance → “Not enough!”
- Else subtract & show new balance
Paste your code here when done — I’ll check, debug, or suggest improvements 😄
Any part confusing right now?
- “Explain range() more”
- “Show nested loops example”
- “Why for is better than while most times”
- “Next topic please” (functions — reusable code blocks!)
You’re killing it — loops are where boring repetition dies and productivity begins 🚀
Keep going, champ! 💪
