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.


Python For Loop Quiz

Q1: What does a for loop do?

A. Makes decisions
B. Repeats code
C. Defines functions
D. Imports modules

Q2: What is the output of range(3)?

A. 1 2 3
B. 0 1 2
C. 0 1 2 3
D. None

Q3: What can you iterate over using for?

A. Strings
B. Lists
C. Tuples
D. All of the above

Q4: What does break do in a loop?

A. Repeats loop
B. Skips value
C. Ends loop
D. Pauses loop

Q5: What does continue do in a loop?

A. Stops loop
B. Skips current iteration
C. Exits function
D. Returns variable

Q6: Which function is commonly used in for loops to generate numbers?

A. list()
B. str()
C. range()
D. len()

Q7: What is the default start value of range(n)?

A. 1
B. 0
C. -1
D. None

Q8: How do you define a step of 2 in range()?

A. range(1, 10, 2)
B. range(1, 10 + 2)
C. range(1..10,2)
D. step(2)

Q9: Can you use else with for loops in Python?

A. Yes
B. No
C. Only in functions
D. Only with if

Q10: Which loop is used for fixed iteration in Python?

A. while
B. repeat
C. for
D. if

Go Back Top