Python18 min read
Python Loops
Learn for loops, while loops, and loop control statements (break/continue) with output examples and mental models.
Emily Davis
August 31, 2025
10.8k223
Loops repeat code. They are essential for handling collections and repeating tasks.
Two main loop types:
for: repeat over items (lists, ranges)while: repeat until a condition becomes false
for loop (over a list)
cities = ["Boston", "Portland", "Atlanta"]
for city in cities:
print(city)
Expected output:
Boston
Portland
Atlanta
for loop with range()
for i in range(5):
print(i)
Expected output:
0
1
2
3
4
while loop (repeat until condition changes)
count = 0
while count < 3:
print(count)
count += 1
Expected output:
0
1
2
break and continue
break stops the loop:
for i in range(6):
if i == 3:
break
print(i)
Expected output:
0
1
2
continue skips one iteration:
for i in range(5):
if i == 2:
continue
print(i)
Expected output:
0
1
3
4
Graph: loop cycle
flowchart TD
A[Start] --> B{Condition/Items left?}
B -->|Yes| C[Run body]
C --> D[Update / next item]
D --> B
B -->|No| E[Exit]
In the next lesson, you will learn functions so your code becomes reusable and easier to maintain.
#Python#Beginner#Loops