Back to the main roadmap | Previous lesson: Functions & Built-in Functions
A loop lets you repeat a block of code without writing it over and over again.
Instead of writing:
print("Hi")
print("Hi")
print("Hi")
print("Hi")
print("Hi")You can simply tell Python:
Repeat this 5 times.
Imagine washing 10 dishes.
You don't invent a new set of instructions for every dish.
You simply repeat the same steps:
- Pick up the dish
- Scrub it
- Rinse it
- Put it away
A loop works the same way.
Python has two main types of loops:
forloopwhileloop
A for loop goes through items one at a time.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)apple
banana
cherry
Python reads it like this:
- Take the first item →
"apple" - Store it in
fruit - Run the code inside the loop
- Repeat for every remaining item
After the last item, the loop ends automatically.
Strings are collections of characters.
Python can loop through each character.
for letter in "cat":
print(letter)c
a
t
The range() function generates numbers.
for i in range(5):
print(i)0
1
2
3
4
Notice that 5 is not included.
range(5) produces five numbers, starting from 0.
range(5)Output
0 1 2 3 4
range(2, 6)Output
2 3 4 5
range(0, 10, 2)Output
0 2 4 6 8
The third value is called the step.
The variable inside the loop changes every iteration.
for i in range(5):
print("Round:", i)Round: 0
Round: 1
Round: 2
Round: 3
Round: 4
Sometimes you need the position (index) instead of the item.
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(i, "-", fruits[i])0 - apple
1 - banana
2 - cherry
Here:
iis the index.fruits[i]gets the item at that position.
enumerate() gives both the index and the item.
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(i, "-", fruit)0 - apple
1 - banana
2 - cherry
This is cleaner than using range(len()).
A while loop keeps running as long as a condition is True.
count = 0
while count < 5:
print(count)
count += 10
1
2
3
4
- Check if
count < 5 - If True, run the loop
- Increase
count - Check the condition again
- Stop when the condition becomes False
If you forget to update the condition, the loop never ends.
count = 0
while count < 5:
print(count)Since count never changes, the condition is always True.
The loop runs forever.
To stop an infinite loop in the terminal, press:
Ctrl + C
Sometimes you intentionally want a loop to run forever until the user decides to stop.
while True:
answer = input("Type 'quit' to stop: ")
if answer == "quit":
break
print("You typed:", answer)The loop stops only when break is executed.
break immediately exits the loop.
for i in range(10):
if i == 5:
break
print(i)0
1
2
3
4
When i becomes 5, the loop ends immediately.
continue skips the current iteration and moves to the next one.
for i in range(5):
if i == 2:
continue
print(i)0
1
3
4
The number 2 is skipped.
| Keyword | What It Does |
|---|---|
break |
Stops the loop completely |
continue |
Skips only the current iteration |
A nested loop is a loop inside another loop.
for i in range(3):
for j in range(2):
print(i, j)0 0
0 1
1 0
1 1
2 0
2 1
The inner loop runs completely for every iteration of the outer loop.
A loop can also have an else block.
The else block executes only if the loop finishes normally (without break).
for i in range(5):
print(i)
else:
print("Loop finished normally.")0
1
2
3
4
Loop finished normally.
Create a file named lesson_loops.py.
# 1. Basic for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# 2. range()
for i in range(1, 6):
print("Count:", i)
# 3. enumerate()
for i, fruit in enumerate(fruits):
print(i, "-", fruit)
# 4. while loop
count = 0
while count < 5:
print("While count:", count)
count += 1
# 5. break and continue
for i in range(10):
if i == 7:
break
if i % 2 == 0:
continue
print("Odd number:", i)
# 6. Nested loop
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} x {j} = {i*j}")apple
banana
cherry
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
0 - apple
1 - banana
2 - cherry
While count: 0
While count: 1
While count: 2
While count: 3
While count: 4
Odd number: 1
Odd number: 3
Odd number: 5
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
- Loops repeat code automatically.
- Python has two loop types:
forandwhile. - Use
forwhen looping through a sequence. - Use
whilewhen repeating until a condition becomes False. range()generates a sequence of numbers.enumerate()provides both the index and the value.breakexits the loop immediately.continueskips the current iteration.- Nested loops are loops inside other loops.
- A loop's
elseblock runs only if the loop completes withoutbreak.
Next Lesson: Lists, Tuples, and Sets