For Loop in Python
-
This lesson explains how to use the for loop in Python to iterate over lists, strings, and ranges with practical examples.
What is a Loop?
A loop in Python is a programming structure used to repeat a block of code multiple times until a specific condition is met.
👉 Instead of writing the same code again and again, loops automate repetition.
Why Do We Use Loops?
We use loops to:
Avoid writing repeated code
Save time and reduce code length
Perform repetitive tasks efficiently
Work with collections (lists, strings, numbers)
Automate processes
Problem Without Loop
If you want to print numbers from 1 to 5 without a loop, you must write:
print(1)
print(2)
print(3)
print(4)
print(5)
This is time-consuming and not practical.
Solution Using Loop
for i in range(1, 6):
print(i)
- Short
- Clean
- Efficient
Real-Life Examples of Loops
Where Are Loops Used?
Loops are commonly used in:
Printing patterns
Processing lists and strings
Calculations (sum, average)
Login attempts
Games and animations
Data processing
Types of Loops in Python
Python supports three main looping statements:
for loop
while loop
nested loops
Loop Control Statements
Flow of Loop Execution
Loop starts
Condition is checked
Code block executes
Update happens
Condition re-checked
Loop ends when condition is False
Advantages of Loops
- Reduces code repetition
- Improves readability
- Easy to maintain
- Efficient execution
What is a for Loop?
The for loop in Python is used to repeat a block of code for each item in a sequence such as:
range of numbers
list
string
tuple
dictionary
The loop runs once for each value in the sequence.
Why Use a for Loop?
We use a for loop when:
The number of repetitions is known
We want to iterate over data
Repetitive tasks must be automated
Clean and readable code is required
Syntax of for Loop
General Syntax
for variable in sequence:
statement(s)
Syntax Explanation
Using range() with for Loop
range() Function
range(start, stop, step)
start → starting value (default 0)
stop → ending value (excluded)
step → increment/decrement (default 1)
Printing Numbers Using for Loop
This program prints numbers from 1 to 5 using a for loop.
for i in range(1, 6):
print(i)
Repeating Message Using for Loop
This program prints a welcome message 3 times using a for loop.
for count in range(3):
print("Welcome to Python")
Displaying List Items Using for Loop
This program prints each element from a list using a for loop.
fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)
Printing Each Character Using for Loop
This program prints each character of a string one by one.
name = "Python"
for ch in name:
print(ch)
Printing Even Numbers Using for Loop
This program prints even numbers between 2 and 10 using a step value.
for i in range(2, 11, 2):
print(i)
Nested for Loop Demonstration
This program uses a loop inside another loop to show nested iteration.
for i in range(1, 4):
for j in range(1, 3):
print(i, j)
What is else with for Loop?
In Python, a for loop can have an else block.
The else block is executed:
✅When the loop completes normally
❌ Not executed if the loop is stopped using break
👉 This feature is unique to Python and often misunderstood.
Why Use else with for Loop?
We use else with a for loop to:
Confirm that a loop finished without interruption
Handle search operations
Avoid using extra flags (True/False)
Write clean and logical code
Basic Syntax of for-else
for variable in sequence:
statement(s)
else:
statement(s)
Using else with for Loop (No break)
This program demonstrates how the else block executes when the loop finishes normally.
for i in range(1, 6):
print(i)
else:
print("Loop completed successfully")
Explanation
Loop runs from 1 to 5
No break is used
else block executes at the end
Using else with for Loop and break
This program shows that the else block does NOT execute when the loop is terminated using break.
for i in range(1, 6):
if i == 3:
break
print(i)
else:
print("Loop completed")
Explanation
Loop stops when i == 3
break is executed
else block is skipped