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):

Python

Most common ways to use for:

3.1 Classic – using range() (counting numbers)

Python

Output:

text

range(start, stop, step) — very flexible

Python

3.2 Looping over a list (super powerful!)

Python

Output:

text

3.3 With index (when you need position)

Python

Or better (Pythonic way):

Python

4. The while Loop (Repeat while condition is True)

Syntax:

Python

Example – Keep guessing password

Python

Danger – Infinite loop (very common beginner mistake!)

Python

→ 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
Python
Python

6. Combining Loops + Lists + If (real power)

Python

7. Common Beginner Mistakes (from real students)

  1. Forgetting to update variable in while → infinite loop
  2. Off-by-one error with range() → range(1, 10) misses 10
  3. Indentation wrong → loop runs only once or error
  4. Modifying list while looping over it (advanced, but crashes sometimes)
  5. Using = instead of += for counters
  6. 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! 💪

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *