🔁 Python for Loop – Smart Iteration Made Simple | TechTown.in
The for loop is one of the most commonly used tools in Python. It allows you to iterate over a sequence of items — like a list, tuple, string, or even a dictionary — and perform actions on each element.
Whether you’re looping through user data, printing numbers, or processing files, Python’s for loop makes it easy, clean, and powerful.
🧠 What is a for Loop in Python?
A for loop is used to execute a block of code for each item in a sequence.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
🎯 Output:
apple
banana
cherry
🔍 Looping Through Strings
Strings are also iterable — each character is a loop item.
for letter in "TechTown":
print(letter)
🎯 Output:
T
e
c
h
T
o
w
n
🔢 Using for with range()
The range() function generates a sequence of numbers.
for i in range(5):
print(i)
🎯 Output:
0
1
2
3
4
🧠 Want to start from 1?
for i in range(1, 6):
print(i)
🔁 Step Size in range(start, stop, step)
for i in range(0, 10, 2):
print(i)
🎯 Output:
0
2
4
6
8
📦 Looping Through Dictionaries
🔹 Loop through keys:
person = {"name": "Tanmay", "age": 22}
for key in person:
print(key)
🔸 Loop through key-value pairs:
for key, value in person.items():
print(f"{key}: {value}")
⏭️ Using break to Exit Early
for i in range(10):
if i == 5:
break
print(i)
🎯 Output:
0
1
2
3
4
🔄 Using continue to Skip Iteration
for i in range(5):
if i == 2:
continue
print(i)
🎯 Output:
0
1
3
4
🧪 for...else in Python
Python also supports else with loops — it runs only if the loop completes normally (not broken with break).
for i in range(3):
print(i)
else:
print("Finished looping!")
📝 Summary – Python for Loop Cheat Sheet
| Feature | Example | Purpose |
|---|---|---|
| Loop over list | for x in [1,2,3]: | Access each list element |
| Loop over string | for ch in "abc": | Iterate characters |
| Loop with range | for i in range(5): | Number-based loops |
| Step size | range(0, 10, 2) | Custom stepping |
| Break loop | if x == target: break | Stop early |
| Skip iteration | if x < 0: continue | Skip invalid data |
for...else | for x in y: ... else: | Run something when loop ends |
🏁 Final Thoughts
The Python for loop is a powerful and flexible way to automate repetitive tasks. From lists and strings to complex data structures, you’ll use for loops almost everywhere in your Python journey.
Once you master it, you’ll unlock better logic flow, cleaner code, and more efficient problem-solving.
📘 Keep learning Python the easy way at TechTown.in

