Day 5 — Loops in Python
Why Loops Even Exist?
Okay, imagine this:
You have a list of 5 students’ names and you want to greet each one. How would you do it?
You could do this:
students = ["Ankit", "Riya", "John", "Meera", "Arjun"]
print("Hello", students[0])
print("Hello", students[1])
print("Hello", students[2])
print("Hello", students[3])
print("Hello", students[4])Output:
easy, right?
Now imagine 100 names. Would you write print() 100 times?That’s where loops come in. They help you do repetitive work with minimal code.
Let’s break down the most commonly used loop in Python — the for loop.
The for Loop
Syntax:
for item in collection:
# do something with itemLet’s see it in action:
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print("I like", fruit)Visually Representation:
Output:
I like apple
I like banana
I like mangoSo, what’s happening here?
The loop goes through each item in the fruits list one by one.
- First,
fruitbecomes"apple" - Then
"banana" - Then
"mango"
Each time, Python prints "I like" followed by the current item.
Note: The name fruit is just a variable — you can name it anything you like! What matters is that it holds one item from the list at a time. This simple loop lets you print all items with just a few lines of code — no repetition needed!
When should you use a loop?
If you notice that your code is repeating the same task, but only the value or index is changing — that’s your signal to use a loop!
Think of it like this:
If you’re copying and pasting similar lines of code just to change one small part — stop and use a loop instead.
Example:
print("I like apple")
print("I like banana")
print("I like mango")Instead of writing this 3 times, a loop does it in 1 clean block:
for fruit in fruits:
print("I like", fruit)Looping with range() in Python
So far, we’ve looped through lists directly. But what if you want to repeat something a certain number of times — or loop through numbers?
That’s where the range() function comes in.
range()
range() is a built-in Python function that generates a sequence of numbers.
Syntax:
for i in range(n):
# codeThis will loop from 0 to n-1 (not including n).
Think of
range(n)as: “Do this taskntimes.”
👉Example 1:
for i in range(5):
print("Hello", i)Output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4Here, i takes values from 0 to 4. The loop runs 5 times, starting from 0.
👉Example 2: Loop through indexes of a list
Sometimes, you want to loop using indexes, especially when:
- You need both the index and the value
- You want to update or access elements by position
fruits = ["apple", "banana", "mango"]
for i in range(len(fruits)):
print("Index:", i, "→", fruits[i])Output:
Index: 0 → apple
Index: 1 → banana
Index: 2 → mangoHere, len(fruits) gives 3, so range(3) generates 0, 1, 2.
👉Example 3: Custom Start and Stop
for i in range(2, 6):
print(i)Output:
2
3
4
5- Starts at 2
- Stops before 6
So range(start, stop) → includes start, excludes stop.
Get Ratnesh’s stories in your inbox
Join Medium for free to get updates from this writer.
👉Example 4: Step in Range (Skip Numbers)
for i in range(1, 10, 2):
print(i)Output:
1
3
5
7
9Here, we’re jumping by 2 each time — using the third argument called step
range(start, stop, step) is like saying: “Start from this number, stop before that number, and move this many steps at a time.”
When to Use range()?
Use range() when:
- You want to repeat something a specific number of times
- You need to count up or down
- You want to loop through list indexes
- You want more control over how the loop runs
Note: Practice along, people! It’ll help you more than just reading.
Type these examples out and tweak them — that’s how real understanding builds.
🚀 But Wait… There’s a Better Way: enumerate()
So far, we’ve used range() to get both the index and value while looping:
fruits = ["apple", "banana", "mango"]
for i in range(len(fruits)):
print("Index:", i, "→", fruits[i])It works — but there’s a better way to get this done!!
✅ Meet enumerate()
What it does: enumerate() gives you both the index and the value while looping — in a cleaner, more readable way.
Syntax:
for index, value in enumerate(some_list):
# use index and value👉 Example:
fruits = ["apple", "banana", "mango"]
for index, fruit in enumerate(fruits):
print("Index:", index, "→", fruit)`🟢 Output:
Index: 0 → apple
Index: 1 → banana
Index: 2 → mangoClean, right?
This is what most Python pros use — and it's interview-safe and production-friendly.
🔍 When Should You Use enumerate()?
- When you need both the index and the value from a list
- When writing cleaner, more Pythonic loops
- When labeling data in a UI, PDF, or export
Bonus Examples
👉Print all even numbers from 1 to 10:
for i in range(1, 11):
if i % 2 == 0:
print(i)Output:
2
4
6
8
10👉Print each character of a word:
word = "python"
for letter in word:
print(letter)Output:
p
y
t
h
o
n👉Start from a Custom Index
for index, fruit in enumerate(fruits, start=1):
print(f"{index}. {fruit}")🟢 Output:
1. apple
2. banana
3. mangoSuper useful when you want to number items starting from 1 instead of 0.
Mistake to Avoid
This will crash:
for i in range(3):
print(i) # IndentationErrorError Message:
Fix it by indentation:
for i in range(3):
print(i) # Indentation FixedOutput:
🎯Mini Challenge Time!
Note: You need to practice these to level up your Python skills!
1. Multiples of 3 from 1 to 20 🔁
Use range() and if to print only the numbers that are divisible by 3.
2. Favorite Food Indexer 🍕
Create a list of your 5 favorite foods and use range() to print each item with its index.
3. Reverse Loop 🔄
Print numbers from 5 to 1 using range() and a negative step.
4. Create a list of employee names and print them numbered (starting from 1) using enumerate()
Closing Thoughts
Today, you learned one of the most powerful tools in programming — the for loop.
No more writing 100 lines of repetitive code. Python can now handle that with a single loop.
With loops, you’ve officially stepped into automation. You’ve made Python repeat, scale, and simplify your logic — and that’s what modern coding is all about.
🎯 And this is just the beginning.









