Python For Loop


The for loop in Python is used to iterate over a sequence (like a list, tuple, string, or range).


🔹 Syntax

for variable in sequence:
    # code block

🔹 Looping Through a List

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

for fruit in fruits:
    print(fruit)

🔹 Looping Through a String

for letter in "Python":
    print(letter)

🔹 Using the range() Function

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

Output:
0 1 2 3 4


🔹 Specify Start and End in range()

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

Output:
2 3 4 5


🔹 Using Step in range()

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

Output:
1 3 5 7 9


🔹 Else in For Loop

for i in range(3):
    print(i)
else:
    print("Finished")

🔹 Nested For Loop

colors = ["red", "green"]
objects = ["car", "bike"]

for color in colors:
    for obj in objects:
        print(color, obj)

🔹 Break Statement

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

🔹 Continue Statement

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

🧪 Test Yourself with These Examples

Practice using for loop with range, lists, strings, and control flow.


Practice Questions

Q1. Write a Python program to print numbers from 1 to 10 using a for loop.

Q2. Write a Python program to loop through a list of names and print each name.

Q3. Write a Python program to loop through a string and count the number of vowels.

Q4. Write a Python program to print all even numbers between 1 and 20.

Q5. Write a Python program to print each character in the word "Python" using a for loop.

Q6. Write a Python program to use range() to print numbers from 5 to 15.

Q7. Write a Python program to print the multiplication table of 4 using a for loop.

Q8. Write a Python program to use nested for loops to print all combinations of numbers 1–3 and letters A–C.

Q9. Write a Python program to use break to stop a loop when the number reaches 5.

Q10. Write a Python program to use continue to skip number 3 in a loop from 1 to 5.


Go Back Top