1️⃣ range(3)
range(3) generates numbers starting from 0 up to 2.
So the values will be:
0, 1, 2
2️⃣ for Loop Execution
The loop runs three times.
Iteration steps:
| Iteration | Value of i | Output |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 1 | 1 |
| 3 | 2 | 2 |
So the loop prints:
0
1
2
3️⃣ else with for Loop
In Python, a for loop can have an else block.
The else block executes only if the loop finishes normally (no break statement).
Since there is no break in this loop, the else block runs.
4️⃣ Final Output
0
1
2
Done
⚡ Important Concept
If we add break, the else will not run.
Example:
for i in range(3):
print(i)
break
else:
print("Done")
Output:
0
Done will not print because the loop stopped using break.


0 Comments:
Post a Comment