Loops (for and while)
Loops in Python
Section titled “Loops in Python”Introduction
Section titled “Introduction”Loops let you execute a block of code repeatedly. Python has two loop types: for (iterating over sequences) and while (condition-based).
For Loops
Section titled “For Loops”# Iterating over a sequencefruits = ["apple", "banana", "cherry"]for fruit in fruits: print(fruit)
# Iterating over a stringfor char in "Hello": print(char)
# Using range()for i in range(5): # 0, 1, 2, 3, 4 print(i)
for i in range(2, 10, 3): # 2, 5, 8 print(i)While Loops
Section titled “While Loops”count = 0while count < 5: print(count) count += 1
# Infinite loop (use with caution!)while True: user_input = input("> ") if user_input == "quit": breakBreak and Continue
Section titled “Break and Continue”# break — exit loop entirelyfor i in range(10): if i == 5: break print(i) # 0, 1, 2, 3, 4
# continue — skip to next iterationfor i in range(10): if i % 2 == 0: continue print(i) # 1, 3, 5, 7, 9Else Clause on Loops
Section titled “Else Clause on Loops”# else runs if loop completed normally (not via break)for n in range(2, 10): for divisor in range(2, n): if n % divisor == 0: print(f"{n} is not prime") break else: print(f"{n} is prime!")enumerate() — Indexed Loops
Section titled “enumerate() — Indexed Loops”fruits = ["apple", "banana", "cherry"]for i, fruit in enumerate(fruits, start=1): print(f"{i}. {fruit}")# 1. apple# 2. banana# 3. cherryzip() — Parallel Iteration
Section titled “zip() — Parallel Iteration”names = ["Alice", "Bob", "Charlie"]scores = [85, 92, 78]grades = ["B", "A", "C"]
for name, score, grade in zip(names, scores, grades): print(f"{name}: {score} ({grade})")Best Practices
Section titled “Best Practices”- Prefer
foroverwhilewhen iterating over sequences - Use
enumerate()instead of manual index tracking - Use
zip()for parallel iteration instead of indexing - Avoid modifying a list while iterating — iterate over a copy instead
- Use
elseon loops sparingly — it’s powerful but can be confusing
Practice Exercises
Section titled “Practice Exercises”Exercise 1: Print a multiplication table (1-10) using nested loops.
Exercise 2: Find all prime numbers between 1 and 100.
Exercise 3: Implement a simple text-based menu that keeps running until the user chooses to exit.