🔁 Python For Loop — Iterating Through Sequences Easily

Introduction 🌟

A for loop is used to iterate over a sequence such as a list, tuple, string, set, dictionary, or a range of numbers. It is one of the most commonly used loops in Python for processing collections and repeating tasks a specific number of times.

Note

💡 A for loop automatically fetches each item from the sequence — no need for indexing manually!

1. Basic For Loop Structure 🧱

basic_for.py

for i in range(5):
    print(i)

✔️ Iterates from 0 to 4 (5 values)
✔️ The loop runs once for each number

2. Iterating Through a List 📦

for_list.py

fruits = ["apple", "banana", "mango"]

for fruit in fruits:
    print(fruit)

3. Iterating Through a String 🔤

for_string.py

for char in "Python":
    print(char)

4. Iterating Through a Tuple 🔗

for_tuple.py

colors = ("red", "green", "blue")

for color in colors:
    print(color)

5. Iterating Through a Set 🎯

for_set.py

ids = {101, 102, 103}

for item in ids:
    print(item)

Note

⚠️ Sets do not maintain order — output may vary.

6. Iterating Through a Dictionary 🗂️

Keys

for_dict_keys.py

student = {"name": "Sathish", "age": 25}

for key in student:
    print(key)

Values

for_dict_values.py

for value in student.values():
    print(value)

Key–Value Pairs

for_dict_items.py

for key, value in student.items():
    print(key, "=", value)

7. Using Range() Function 🎯

range() is used to generate a sequence of numbers.

range(n)

range_n.py

for i in range(5):
    print(i)

range(start, end)

range_start_end.py

for i in range(2, 6):
    print(i)

range(start, end, step)

range_step.py

for i in range(1, 10, 2):
    print(i)

8. For Loop With Break 🛑

for_break.py

for i in range(10):
    if i == 5:
        break
    print(i)

Note

✔️ Stops loop when i becomes 5.

9. For Loop With Continue 🔄

for_continue.py

for i in range(5):
    if i == 2:
        continue
    print(i)

Note

✔️ Skips printing the value 2.

10. Nested For Loops 🪜

nested_for.py

for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)

11. Looping With Else Block 🎯

The else block runs after the loop finishes normally (without break).

for_else.py

for i in range(3):
    print(i)
else:
    print("Loop finished")

12. Iterating With Enumerate() 🔢

enumerate() provides both index and value.

enumerate_example.py

names = ["Sathish", "Kumar", "Python"]

for index, name in enumerate(names):
    print(index, name)

13. Iterating Using Zip() 🔗

zip_example.py

names = ["A", "B", "C"]
scores = [90, 85, 92]

for name, score in zip(names, scores):
    print(name, score)

14. Real-World Example 🌍

real_world_example.py

emails = ["a@gmail.com", "b@gmail.com", "c@gmail.com"]

for email in emails:
    if "@" in email:
        print(email, "is valid")
    else:
        print(email, "is invalid")

Conclusion 🎉

>>“For loops let you process sequences effortlessly — clean, fast, and powerful.” ✨

You now fully understand the for loop in Python! Want the next tutorial? Try Range(), Loop Control (break/continue/pass), Nested Loops, or List Comprehensions. Just tell me! 😊