-
Hajipur, Bihar, 844101
The for
loop in Python is used to iterate over a sequence (like a list, tuple, string, or range).
for variable in sequence:
# code block
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
for letter in "Python":
print(letter)
range()
Functionfor i in range(5):
print(i)
Output:0 1 2 3 4
range()
for i in range(2, 6):
print(i)
Output:2 3 4 5
range()
for i in range(1, 10, 2):
print(i)
Output:1 3 5 7 9
for i in range(3):
print(i)
else:
print("Finished")
colors = ["red", "green"]
objects = ["car", "bike"]
for color in colors:
for obj in objects:
print(color, obj)
for i in range(5):
if i == 3:
break
print(i)
for i in range(5):
if i == 2:
continue
print(i)
Practice using for
loop with range
, lists, strings, and control flow.
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.
Q1: What does a for loop do?
Q2: What is the output of range(3)?
Q3: What can you iterate over using for?
Q4: What does break do in a loop?
Q5: What does continue do in a loop?
Q6: Which function is commonly used in for loops to generate numbers?
Q7: What is the default start value of range(n)?
Q8: How do you define a step of 2 in range()?
Q9: Can you use else with for loops in Python?
Q10: Which loop is used for fixed iteration in Python?