🐍beginner
Python Loops — Repeating Things Easily
Learn how to use for loops and while loops to make Python repeat tasks for you — from counting to processing lists of data.
Why Loops Are Awesome
Imagine you're a teacher and you need to print 'Good morning!' for each of your 30 students. Without loops, you'd have to write print() 30 times. That's crazy!
Loops let you say 'repeat this code' a certain number of times, or for every item in a list. They save you from writing the same code over and over. Almost every real program uses loops — from games (repeat every frame) to apps (check every message) to websites (show every product).
For Loops — Repeat for Each Item
python
# Loop through a list of items
fruits = ["apple", "banana", "mango", "grape"]
for fruit in fruits:
print(f"I love {fruit}!")
# Output:
# I love apple!
# I love banana!
# I love mango!
# I love grape!
# Loop through numbers with range()
for i in range(5):
print(f"Count: {i}")
# Prints 0, 1, 2, 3, 4
# range(start, stop, step)
for i in range(2, 11, 2):
print(i)
# Prints 2, 4, 6, 8, 10 (even numbers!)How For Loops Work
Think of a for loop like going through a bag of candy. You reach in, grab one piece, do something with it (eat it!), then reach in for the next piece. You keep going until the bag is empty.
• 'for fruit in fruits:' means 'for each fruit in my fruits list, do this...'
• The variable 'fruit' changes each time — first it's 'apple', then 'banana', and so on
• The indented code runs once for EACH item
• When there are no more items, the loop stops automatically
While Loops — Repeat While Something is True
python
# While loops keep going as long as a condition is true
lives = 3
while lives > 0:
print(f"Lives remaining: {lives}")
lives = lives - 1 # lose a life each round
print("Game over!")
# Output:
# Lives remaining: 3
# Lives remaining: 2
# Lives remaining: 1
# Game over!
# Counting up
count = 1
while count <= 5:
print(f"{count} x 7 = {count * 7}")
count += 1 # same as count = count + 1Loop Tricks: break and continue
python
# break — escape the loop early
for number in range(1, 100):
if number == 5:
print("Found 5! Stopping.")
break
print(number)
# Prints: 1, 2, 3, 4, Found 5! Stopping.
# continue — skip this one, move to the next
for number in range(1, 8):
if number == 4:
continue # skip 4
print(number)
# Prints: 1, 2, 3, 5, 6, 7 (no 4!)
# Real example: skip empty names
names = ["Aarav", "", "Priya", "", "Zain"]
for name in names:
if name == "":
continue
print(f"Hello, {name}!")Pro Tip
Watch out for infinite loops! A while loop runs forever if the condition never becomes False. Always make sure something inside the loop changes the condition. If your program freezes, it's probably stuck in an infinite loop — press Ctrl+C to stop it.
Try It Yourself
Write a program that prints the multiplication table for any number. Ask the user for a number, then use a for loop to print that number multiplied by 1 through 12. Example: if they enter 7, print '7 x 1 = 7', '7 x 2 = 14', all the way to '7 x 12 = 84'.
Ready to build?
Put what you learned into practice — pick a project and start coding.
Start Building Free