A loop lets your program repeat actions without writing the same code over and over.
Example in real life: You check your messages every 5 minutes. That’s like a loop — repeat until there are no new messages.
By the end of this lesson, you will:
Understand when and how to use for and while loops.
Use break to exit a loop early.
Use continue to skip part of a loop.
Write and test your own loops.
What is a for loop?
Use a for loop when you know how many times you want something to repeat.
Example use: Print numbers 1 to 5.
🔍 Example 1: Print 0 to 4
# Repeats 5 times, starting at 0
for i in range(5):
print(i)
What it does:
range(5) gives numbers: 0, 1, 2, 3, 4
It runs 5 times
It starts at zero
Try copying this and changing the number in range().
What happens if you change it to range(10)?
What is a while loop?
Use a while loop when you want to repeat something until a condition changes.
Example use: Ask for a password until it’s correct.
🔍 Example 4: Count up to 4
# Keep repeating until count is 5
count = 0
while count < 5:
print(count)
count += 1
Output: 0, 1, 2, 3, 4
🔍 Example 5: Ask for password
# Ask until the correct password is typed
password = ""
while password != "admin123":
password = input("Enter password: ")
It stops only when the user types "admin123".
Try changing the password to something else.
What happens if you don’t change count in a while loop?
What is break?
It ends the loop immediately — even if the condition or range isn’t finished.
🔍 Example 6: Break inside a for loop
# Stop the loop when i reaches 4
for i in range(10):
if i == 4:
break
print(i)
Output: 0, 1, 2, 3
🔍 Example 7: Break inside a while loop
x = 0
while True:
print(x)
x += 1
if x == 5:
break
Output: 0, 1, 2, 3, 4
What is continue?
It skips the rest of that loop step and jumps to the next one.
🔍 Example 8: Skip 3 in a for loop
for i in range(6):
if i == 3:
continue
print(i)
Output: 0, 1, 2, 4, 5
🔍 Example 9: Skip 4 in a while loop
x = 0
while x < 6:
x += 1
if x == 4:
continue
print(x)
Output: 1, 2, 3, 5, 6
Mistake
Why it’s wrong
How to fix it
for i in 5:
You must use range()
for i in range(5):
Infinite loop
Forgot to change the loop variable
Use x += 1 inside the loop
Try running each example above on your computer or online Python tool.
✅ Change the numbers
✅ Change the condition
✅ Add print statements to explain what’s happening
🔗 Practice on W3Schools: Loops Tutorials (While loops and For loops) – Learn how to use loops in Python.
🔗 Try It Online: Run Your Code Here – Test your Python code instantly.
Print numbers from 1 to 20 using a for loop.
Ask the user to type "go". Use a while loop to keep asking until they write it.
Use a for loop to print numbers from 1 to 10. Stop early if the number is 7.
Use a while loop to count from 1 to 6, but skip the number 4 using continue.
Without looking at the lesson or your own code, write the Four Fun Challenges code on paper.
When you have done it, compare your code to the correct code. Did you make any errors?
Correct your code and try to remember what your mistake were so you dont make them again.