Master Python loops with practical examples. Learn for loops, while loops, and automation techniques to make your Python programs efficient and powerful.
🕒 7 min read
Hey there! 👋 Remember when we learned about if/else statements and made our programs smart? Well, today we’re going to make them powerful by teaching them how to repeat tasks!
Think about your daily life:
Python loops work exactly the same way! Let me show you how to automate the boring stuff. 🔄
Imagine you need to print numbers 1 through 5:
# ❌ The tedious way
print(1)
print(2)
print(3)
print(4)
print(5)
# ✅ The loop way
for number in range(1, 6):
print(number)
See the difference? 5 lines vs 2 lines! Now imagine doing this for 1000 numbers… loops save you from repetitive strain injury! 😅
For Loops: Your Counting Machine For loops are perfect when you know how many times you want to repeat something.
Basic For Loop with Range
# Count from 0 to 4
for i in range(5):
print(f"Count: {i}")
# Output:
# Count: 0
# Count: 1
# Count: 2
# Count: 3
# Count: 4
🔍 Range Explained:
range(5) = 0, 1, 2, 3, 4
range(1, 6) = 1, 2, 3, 4, 5
range(1, 10, 2) = 1, 3, 5, 7, 9 (steps of 2)
Real-World Example: Daily Reminders
python
# Set up daily reminders
tasks = ["Drink water", "Take break", "Check posture", "Stretch"]
print("📋 Your daily reminders:")
for task in tasks:
print(f"• {task}")
# Output:
# 📋 Your daily reminders:
# • Drink water
# • Take break
# • Check posture
# • Stretch
While Loops: The “Keep Going” Loop While loops repeat as long as a condition is true. Perfect for when you don’t know how many times you’ll need to loop!
Basic While Loop
# Countdown timer
countdown = 5
while countdown > 0:
print(f"🚀 {countdown}...")
countdown -= 1 # Don't forget this!
print("Blast off! 🎉")
# Output:
# 🚀 5...
# 🚀 4...
# 🚀 3...
# 🚀 2...
# 🚀 1...
# Blast off! 🎉
⚠️ Warning: Forgetting to update your condition can create an infinite loop!
# ❌ DANGER: Infinite loop!
# countdown = 5
# while countdown > 0:
# print("This will run forever!")
# 🛑 Always update your condition!
Real-World Example: User Input Validation
python
# Keep asking until we get valid input
age = None
while age is None:
try:
user_input = input("How old are you? ")
age = int(user_input)
if age <= 0:
print("Please enter a valid age!")
age = None
except ValueError:
print("That's not a number! Try again.")
print(f"Great! You are {age} years old.")
Loop Control: Be the Boss of Your Loops Sometimes you need to break out of loops or skip certain iterations.
Break: Emergency Exit
# Search for a number, stop when found
numbers = [3, 7, 2, 9, 4, 6]
target = 9
for number in numbers:
print(f"Checking {number}...")
if number == target:
print("🎯 Found it!")
break # Exit the loop immediately
print("Not this one...")
# Output:
# Checking 3...
# Not this one...
# Checking 7...
# Not this one...
# Checking 2...
# Not this one...
# Checking 9...
# 🎯 Found it!
Continue: Skip to Next Round
# Print only even numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Even numbers:")
for num in numbers:
if num % 2 != 0: # If number is odd
continue # Skip to next iteration
print(num)
# Output:
# Even numbers:
# 2
# 4
# 6
# 8
# 10
Practical Project: Number Guessing Game Let’s build a complete game using loops!
import random
def guessing_game():
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 7
print("🎮 Welcome to the Number Guessing Game!")
print(f"I'm thinking of a number between 1 and 100.")
print(f"You have {max_attempts} attempts to guess it!")
while attempts < max_attempts:
try:
guess = int(input(f"\nAttempt {attempts + 1}/{max_attempts}: Your guess? "))
attempts += 1
if guess < secret_number:
print("📈 Too low! Try higher.")
elif guess > secret_number:
print("📉 Too high! Try lower.")
else:
print(f"🎉 CONGRATULATIONS! You guessed it in {attempts} attempts!")
break
except ValueError:
print("❌ Please enter a valid number!")
else: # This runs if while loop completes without break
print(f"💔 Game over! The number was {secret_number}.")
# Start the game
guessing_game()
Nested Loops: Loops Within Loops
Sometimes you need to loop inside another loop - like going through rows and columns!
python
# Multiplication table
print("Multiplication Table (1-5):")
print(" ", end="")
for i in range(1, 6):
print(f"{i:4}", end="")
print()
for row in range(1, 6):
print(f"{row}: ", end="")
for col in range(1, 6):
product = row * col
print(f"{product:4}", end="")
print() # New line after each row
# Output:
# Multiplication Table (1-5):
# 1 2 3 4 5
# 1: 1 2 3 4 5
# 2: 2 4 6 8 10
# 3: 3 6 9 12 15
# 4: 4 8 12 16 20
# 5: 5 10 15 20 25
Common Loop Patterns You'll Use Every Day
Pattern 1: Processing Lists
python
# Calculate average grade
grades = [85, 92, 78, 90, 88]
total = 0
for grade in grades:
total += grade
average = total / len(grades)
print(f"📊 Average grade: {average:.1f}")
Pattern 2: Building New Lists
python
# Create list of squares
numbers = [1, 2, 3, 4, 5]
squares = []
for num in numbers:
squares.append(num ** 2)
print(f"Numbers: {numbers}")
print(f"Squares: {squares}")
Pattern 3: Looping with Index
python
# Loop with index and value
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index + 1}. {fruit}")
# Output:
# 1. apple
# 2. banana
# 3. cherry
Debugging Loops: Common Issues Fixed
Problem 1: Off-by-One Errors
python
# ❌ Wrong (misses last number)
for i in range(1, 5):
print(i) # Prints 1,2,3,4 (missing 5)
# ✅ Correct
for i in range(1, 6):
print(i) # Prints 1,2,3,4,5
Problem 2: Modifying List While Looping
python
# ❌ Dangerous (can skip elements)
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
numbers.remove(num) # Bad idea!
# ✅ Safe (create new list or loop backwards)
numbers = [1, 2, 3, 4, 5]
even_numbers = [num for num in numbers if num % 2 == 0]
Your Turn: Practice Exercises
Exercise 1: FizzBuzz
python
# Print numbers 1-100, but:
# - For multiples of 3, print "Fizz"
# - For multiples of 5, print "Buzz"
# - For multiples of both, print "FizzBuzz"
# - Otherwise, print the number
# Your code here!
Exercise 2: Password Strength Checker
python
# Keep asking for password until it meets:
# - At least 8 characters
# - Contains a number
# - Contains uppercase letter
# Then print "Password accepted!"
# Your code here!
Advanced Loop Techniques (Preview)
List Comprehensions
python
# Instead of:
squares = []
for x in range(10):
squares.append(x**2)
# You can write:
squares = [x**2 for x in range(10)]
We’ll cover this in detail in our advanced Python posts!
What’s Next? Python Functions! In our next Python post, we’ll learn about functions - how to create reusable code blocks that make your programs organized and efficient! We’ll cover:
Defining and calling functions
Parameters and return values
Scope and namespaces
Building a complete project with functions
Your Mission Create a “Daily Habit Tracker” that:
Asks for 5 habits you want to track
Loops through each day of the week
For each day, asks if you completed each habit
Calculates and displays your weekly success rate
Bonus: Add a streak counter that shows how many days in a row you’ve maintained all habits!
Wrapping Up Today you learned how to make Python work for you! You can now:
✅ Use for loops to iterate through sequences
✅ Use while loops for conditional repetition
✅ Control loops with break and continue
✅ Build practical projects and games
✅ Avoid common loop pitfalls
Remember: Loops are your automation superpower. Every time you find yourself repeating code, ask: “Can a loop do this for me?”
Practice tip: Take any repetitive task in your life and think about how you could automate it with Python loops!
Happy coding! 🔄🐍
Stuck on any loop concepts? Built something cool with loops? Share your code in the comments - I’d love to see what you create!
Comments & Discussion
Join the conversation using your GitHub account. Comments are powered by Utterances.