After completing this chapter, you will be able to:
- Understand the
forloop. - Explain why the
forloop is used. - Learn the
range()function. - Iterate over sequences using the
forloop. - Write programs using the
forloop. - Differentiate between
whileandforloops.
In programming, there are situations where we need to execute a block of code repeatedly for a known number of times.
For example:
- Print numbers from 1 to 10.
- Display a multiplication table.
- Print each character of a string.
- Display all elements of a list.
Instead of writing the same statement repeatedly, Python provides the for loop.
A for loop is an iterative control statement used to execute a block of code repeatedly by iterating over a sequence or a range of values.
The for loop is used when:
- The number of iterations is known.
- Iterating over strings.
- Iterating over lists.
- Iterating over tuples.
- Iterating over dictionaries.
- Processing collections of data.
for variable in sequence:
statements Start
│
▼
Select Sequence
│
▼
Next Element?
/ \
Yes No
│ │
▼ ▼
Execute Block Stop
│
▼
Next Element
- Select a sequence.
- The loop variable receives the first value.
- Execute the loop body.
- Move to the next value.
- Repeat until all values are processed.
- Exit the loop.
The range() function generates a sequence of numbers.
It is commonly used with the for loop.
range(stop)or
range(start, stop)or
range(start, stop, step)Example
for number in range(5):
print(number)Output
0
1
2
3
4
Example
for number in range(1, 6):
print(number)Output
1
2
3
4
5
Example
for number in range(2, 11, 2):
print(number)Output
2
4
6
8
10
Write a Python program to print numbers from 1 to 5.
# Print numbers from 1 to 5
for number in range(1, 6):
print(number)1
2
3
4
5
START
FOR number from 1 to 5
Display number
END FOR
STOP
for number in range(1, 6):The range(1, 6) function generates numbers from 1 to 5.
The loop variable number stores one value at a time.
print(number)Displays the current value of number.
for number in range(2, 11, 2):
print(number)2
4
6
8
10
for number in range(1, 11):
print("5 x", number, "=", 5 * number)5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
name = "Python"
for character in name:
print(character)P
y
t
h
o
n
numbers = [10, 20, 30, 40, 50]
for number in numbers:
print(number)10
20
30
40
50
The for loop is used in:
- Reading files
- Processing lists
- Displaying records
- Generating reports
- Data Analysis
- Machine Learning
- Automation Scripts
- Easy to write.
- Suitable for known iterations.
- Reduces code duplication.
- Improves readability.
- Works with all iterable objects.
| while Loop | for Loop |
|---|---|
| Used when iterations are unknown. | Used when iterations are known. |
| Condition-based. | Sequence-based. |
| Manual update required. | Automatic iteration. |
| More chances of infinite loop. | No infinite loop due to missing update. |
❌ Incorrect
for number in range(5, 1):
print(number)Output
No Output
Reason:
Default step is +1, but 5 is greater than 1.
✅ Correct
for number in range(5, 0, -1):
print(number)❌ Incorrect
for number in range(5)✅ Correct
for number in range(5):A for loop is an iterative statement used to execute a block of code repeatedly by iterating over a sequence or range.
The range() function generates a sequence of numbers used for iteration.
Yes.
Example
for character in "Python":
print(character)A while loop depends on a condition, whereas a for loop iterates over a sequence.
range(stop)range(start, stop)range(start, stop, step)
forloop is used for known iterations.range()generates a sequence of numbers.- Python supports three forms of
range(). forloop works with strings, lists, tuples, sets, and dictionaries.forloop automatically moves to the next element.
In this chapter, you learned:
- What the
forloop is. - Why it is used.
- The
range()function. - Three forms of
range(). - Practical programs.
- Real-world applications.
- Advantages.
- Common mistakes.
- Interview questions.