Back to List

Python Loops β€” for, while, break

Learn Python's for loop, while loop, and break/continue with practical examples.

Beginner
|
7min
|
Verified (2026-07)
Progress0/18 (0%)

Python Loops - for, while, break

After completing this topic

You will be able to create loops with for and while, and control the flow with break and continue.


for loop - Repeating a fixed number of times

python
# Iterating through a list
fruits = ["apple", "banana", "grape"]
for fruit in fruits:
print(fruit)
# apple
# banana
# grape

for variable in iterable: - Iterates through each element of a list, string, range, etc.

range() - Creating a sequence of numbers

python
# range(end) - From 0 to end-1
for i in range(5):
print(i)
# 0, 1, 2, 3, 4
# range(start, end)
for i in range(2, 6):
print(i)
# 2, 3, 4, 5
# range(start, end, step)
for i in range(0, 10, 2):
print(i)
# 0, 2, 4, 6, 8

range(5) creates the sequence 0 to 4 (exclusive of 5). In almost all programming languages, ranges are conventionally "end-exclusive".


while loop - Repeating while a condition is true

python
count = 0
while count < 5:
print(f"count = {count}")
count += 1
# count = 0
# count = 1
# count = 2
# count = 3
# count = 4

for is suitable when you "know how many times to repeat," and while is suitable when you "don't know when it will end."

Beware of infinite loops

python
# ❌ This will never stop
count = 0
while count < 5:
print(count)
# If you forget `count += 1` β†’ infinite loop!
# Intentional infinite loop (used in servers, games, etc.)
while True:
command = input("> ")
if command == "quit":
break
print(f"Input: {command}")

When using a while loop, always check "when will this loop end?"


break and continue

break - Exit the loop

python
# Exit immediately when 3 is found
for i in range(10):
if i == 3:
print("Found it!")
break
print(i)
# 0
# 1
# 2
# Found it!

break immediately exits the nearest loop.

continue - Skip the current iteration

python
# Skip even numbers
for i in range(6):
if i % 2 == 0:
continue
print(i)
# 1
# 3
# 5

continue does not end the loop, but skips the current iteration and goes to the next iteration.


Nested loops

python
# Multiplication table for 2 and 3
for dan in range(2, 4):
print(f"--- {dan} times ---")
for i in range(1, 10):
print(f"{dan} x {i} = {dan * i}")

You can put a loop inside another loop. The inner loop executes completely for each iteration of the outer loop.


enumerate - Get both index and value

python
names = ["Kim Hoon", "Lee Soo", "Park Jin"]
# When you need the index
for i, name in enumerate(names):
print(f"{i}: {name}")
# 0: Kim Hoon
# 1: Lee Soo
# 2: Park Jin

enumerate() creates a tuple of (index, value). It's cleaner than for i in range(len(names)).


Criteria for choosing between for and while

SituationRecommendation
Iterating through a list/rangefor
Fixed number of iterationsfor + range()
Condition-based repetitionwhile
Waiting for user inputwhile True + break

In most cases, you will use for. Use while when the "ending condition is not data, but a state."


πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...