Python Looping Constructs: For, While, and Loop Control Statements
Loops are essential for automating repetitive tasks in Python. In this guide, you’ll learn how to use for and while loops, control flow with break, continue, and pass, and apply these concepts to real-world scenarios.
For Loops with range()
The range() function generates sequences for iterating with for loops. It has three forms:
1. range(stop)
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
2. range(start, stop)
for i in range(2, 6):
print(i) # Output: 2, 3, 4, 5
3. range(start, stop, step)
for i in range(0, 10, 2):
print(i) # Output: 0, 2, 4, 6, 8
While Loops
while loops repeat code as long as a condition is True:
count = 0
while count < 3:
print("Count is", count)
count += 1 # Output: 0, 1, 2
Loop Control Statements
1. break: Exit the Loop Immediately
for num in [1, 2, 3, 4, 5]:
if num == 3:
break # Stops the loop
print(num) # Output: 1, 2
2. continue: Skip to the Next Iteration
for num in range(5):
if num % 2 == 0:
continue # Skip even numbers
print(num) # Output: 1, 3
3. pass: Placeholder for Future Code
for num in range(3):
pass # Do nothing (used as a placeholder)
When to Use Loop Control
- break: Stop early when a condition is met (e.g., finding the first match).
- continue: Skip unwanted iterations (e.g., invalid inputs).
- pass: Temporarily fill empty code blocks during development.
Practice Work
Exercise 1: Fix the Infinite Loop
Correct this while loop to avoid running infinitely:
count = 0
while count < 5:
print(count)
count = 0
while count < 5:
print(count)
count += 1
Exercise 2: Use continue
Modify this loop to skip numbers divisible by 3:
for num in range(10):
print(num)
for num in range(10):
if num % 3 == 0:
continue
print(num)
Exercise 3: Use break
Stop the loop when the number 5 is found:
numbers = [1, 3, 5, 7, 9]
for num in numbers:
print(num)
numbers = [1, 3, 5, 7, 9]
for num in numbers:
if num == 5:
break
print(num)
Workshop: Real-World Applications
Workshop 1: Number Guessing Game
Use a while loop to let the user guess a secret number (e.g., 7).
secret = 7
while True:
guess = int(input("Guess a number: "))
if guess == secret:
print("Correct!")
break
print("Try again.")
Workshop 2: Filter Invalid Data
Use continue to skip negative numbers in a list:
data = [5, -2, 10, -8, 3]
for num in data:
# Your code here
print(num)
data = [5, -2, 10, -8, 3]
for num in data:
if num < 0:
continue
print(num)
Workshop 3: Password Attempt Limiter
Allow 3 login attempts using a while loop and break:
attempts = 0
correct_password = "admin123"
while attempts < 3:
password = input("Enter password: ")
if password == correct_password:
print("Access granted!")
break
attempts += 1
else:
print("Too many failed attempts.")
Best Practices
- Avoid Infinite Loops: Ensure
whileloops have an exit condition. - Prefer for over while: Use
forwhen iterating over known sequences. - Use Descriptive Variable Names: e.g.,
attemptsinstead ofn.
Conclusion
Mastering loops and control statements unlocks the power of automation in Python. Practice with the exercises and workshops to build confidence in using for, while, break, and continue effectively.
Next Steps: Explore Python List Comprehensions or Python Functions.

