In the previous post, we learned how conditional statements allow Python programs to make decisions. Now let's learn how to repeat tasks efficiently using loops.
📖 Phase 1: Programming Fundamentals
📌 Topic 10: Loops — for and while
Loops are used to execute a block of code repeatedly.
Imagine you need to print numbers from 1 to 100. Writing print() 100 times would be inefficient. A loop lets you do it with just a few lines of code.
Why Do We Need Loops?
Loops help us:
• Repeat tasks automatically.
• Process large amounts of data.
• Iterate through lists and other collections.
• Automate repetitive operations.
• Reduce duplicate code.
1. for Loop
A for loop is commonly used when you want to iterate over a sequence or a known range of values.
Example:
for i in range(5):
print(i)
Output:
0
1
2
3
4
Notice that range(5) starts from 0 and stops before 5.
Using range()
You can specify a starting point and step.
for i in range(1, 11):
print(i)
Output:
1
2
3
4
5
6
7
8
9
10
With a step:
for i in range(2, 11, 2):
print(i)
Output:
2
4
6
8
10
2. Looping Through a List
You can directly iterate through a list.
fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)
Output:
Apple
Banana
Mango
3. while Loop
A while loop executes as long as a condition remains True.
Example:
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Here, the loop continues until count <= 5 becomes False.
⚠️ Infinite Loops
Be careful with while loops.
This loop never stops:
count = 1
while count <= 5:
print(count)
Why?
Because count never changes, so the condition always remains True. Always make sure the condition can eventually become False.
4. break
break immediately stops the loop.
for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1
2
3
4
5. continue
continue skips the current iteration and moves to the next one.
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
The number 3 is skipped.
6. Nested Loops
A loop can exist inside another loop.
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
Nested loops are commonly used when working with grids, matrices, and combinations of data.
for vs while
Use a for loop when:
👉 You want to iterate through a sequence or range.
Use a while loop when:
👉 You want to continue running code until a condition changes.
Real-World AI Example
Loops are extremely common in AI and Data Science.
For example, you may need to process multiple files:
files = ["data1.csv", "data2.csv", "data3.csv"]
for file in files:
print("Processing:", file)
Output:
Processing: data1.csv
Processing: data2.csv
Processing: data3.csv
The same concept can be used when processing datasets, documents, images, API responses, or multiple AI model outputs.
Common Beginner Mistakes
❌ Creating an infinite while loop.
❌ Forgetting to update the counter.
❌ Misunderstanding the stopping point of range().
❌ Using break when you actually need continue.
Key Takeaways
• Loops allow you to repeat code efficiently.
• for loops are commonly used for sequences and ranges.
• while loops continue while a condition is True.
• break stops a loop completely.
• continue skips the current iteration.
• Nested loops allow you to work with multiple levels of repetition.
➡️ Double Tap ❤️ For More