Iterative Statements Part-2
M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 8 min read
The for Loop
Definition
The for loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a block of code for each item in that sequence.
It is called a Definite Iteration loop because the number of repetitions is known (or determined) before the loop starts — it runs once for each item in the sequence.
In simpler words: The for loop says — 'For every item in this collection, do this task.'
Note - The for loop is also known as: Definite Iteration Loop | Sequence-Controlled Loop | Count-Controlled Loop
while Loop vs for Loop — Key Difference
|
Feature |
while Loop |
for Loop |
|
Type |
Indefinite / Condition-controlled |
Definite / Sequence-controlled |
|
When to use |
Don't know how many times to repeat |
Know the sequence or count in advance |
|
Initialization |
Manual (before loop) |
Automatic (loop variable set by sequence) |
|
Update |
Manual (must write updation) |
Automatic (moves to next item) |
|
Infinite loop risk |
Yes — if update is missing |
No — stops when sequence ends |
|
Entry control |
Yes — condition checked before body |
Yes — checks for next item before body |
Syntax of the for Loop
for variable in sequence:
statement(s) # body of loop
Here:
- variable — the loop variable that takes the value of each item in the sequence one by one.
- sequence — any iterable object: a list, tuple, string, range, etc.
- The body executes once for each item in the sequence.
- The loop automatically moves to the next item after each iteration.
How the for Loop Executes — Step by Step
|
Get sequence list / range / string |
Pick next item assign to variable |
Execute body run statements |
More items? yes → repeat |
Loop ends sequence exhausted |
The range() Function
The range() function is commonly used with for loops to generate a sequence of numbers. It does not store all the numbers in memory — it generates them one at a time (making it very efficient).
Forms of range()
|
Form |
Syntax |
What it produces |
|
1 argument |
range(stop) |
0, 1, 2, ... stop-1 |
|
2 arguments |
range(start, stop) |
start, start+1, ... stop-1 |
|
3 arguments |
range(start, stop, step) |
start, start+step, ... up to stop |
Worked Examples
Example 1 — Loop over a range of numbers
|
for i in range(1, 6): print(i) |
|
OUTPUT |
1 2 3 4 5 |
Explanation
- range(1, 6) generates the sequence: 1, 2, 3, 4, 5 (stops before 6).
- In each iteration, i takes the value of the current number.
- The loop runs 5 times — once for each number in the range.
- No manual initialization or update needed — range() handles it automatically.
Example 2 — Loop over a string
|
for letter in "Python": print(letter) |
|
OUTPUT |
P y t h o n |
Explanation
- A string is a sequence of characters — the for loop visits each character one by one.
- letter takes the value 'P', then 'y', then 't', and so on.
- The loop runs 6 times — once for each character in 'Python'.
Example 3 — Loop over a list
|
fruits = ["Apple", "Banana", "Mango"] for fruit in fruits: print(fruit) |
|
OUTPUT |
Apple Banana Mango |
Explanation
- Lists are sequences — the for loop iterates over each element.
- fruit takes each value: 'Apple', then 'Banana', then 'Mango'.
- The loop runs 3 times — once per item in the list.
Example 4 — Using step in range()
|
# Print even numbers from 2 to 10 for i in range(2, 11, 2): print(i) |
|
OUTPUT |
2 4 6 8 10 |
Explanation
- range(2, 11, 2) starts at 2, stops before 11, and jumps by 2 each time.
- The step value controls how much the loop variable increases each iteration.
- Step can also be negative — to count downwards.
Loop Control Statements
Python provides special keywords to control the flow of a loop from inside the loop body.
1. break Statement
The break statement immediately terminates the loop, regardless of whether the sequence is exhausted or the condition is still True. Execution continues with the statement after the loop.
|
for i in range(1, 10): if i == 5: break print(i) # Output: 1 2 3 4 (stops at 5) |
2. continue Statement
The continue statement skips the rest of the loop body for the current iteration and moves directly to the next item in the sequence.
|
for i in range(1, 6): if i == 3: continue print(i) # Output: 1 2 4 5 (skips 3) |
3. pass Statement
The pass statement does nothing — it is a placeholder used when the loop body is syntactically required but you do not want any action to happen.
|
for i in range(5): pass # placeholder — no action # Loop runs silently — no output |
|
Keyword |
Effect |
When to use |
|
break |
Exits the loop immediately |
Stop early when a condition is met |
|
continue |
Skips current iteration, goes to next |
Skip certain values without stopping |
|
pass |
Does nothing (placeholder) |
Empty loop body as a placeholder |
The for...else Statement
Python allows an else clause after a for loop. The else block runs only if the loop completes normally — that is, it was NOT terminated by a break statement.
|
for i in range(1, 6): print(i) else: print("Loop completed!") # Output: 1 2 3 4 5 Loop completed! |
Important: If a break statement exits the loop, the else block is SKIPPED.
This is useful for search problems: loop through items searching for something, and if the loop finishes without finding it (no break), the else block handles the 'not found' case
Important Notes & Key Rules
Rule 1: The for loop variable is automatically assigned each value from the sequence — no manual initialization or update needed.
Rule 2: range(stop) starts from 0 by default. For example, range(5) gives 0, 1, 2, 3, 4.
Rule 3: The stop value in range() is EXCLUSIVE — range(1, 6) gives 1 to 5, NOT 1 to 6.
Rule 4: You can use a negative step to count backwards: range(10, 0, -1) gives 10, 9, 8, ... 1.
Rule 5: The for loop works on any iterable — lists, tuples, strings, dictionaries, sets, and range objects.
Rule 6: The for loop is an entry-controlled loop — it checks if there is a next item before executing the body.