Python For Loop Explained with Examples for Beginners (2026 Guide)
What is a for Loop in Python?
Introduction
A for loop is one of the most commonly used looping
statements in Python. It is used to execute a block of code repeatedly for each
item in a sequence. Instead of writing the same code multiple times, a for loop
automates repetitive tasks, making programs shorter, cleaner, and easier to
maintain.
Unlike some programming languages where a for loop depends
on initialization, condition, and increment, Python's for loop directly
iterates over objects such as lists, tuples, strings, dictionaries, sets, and
ranges. This makes Python code more readable and beginner-friendly.
Syntax of for Loop
for variable in sequence:
statements
Explanation
- for
→ Python keyword that starts the loop.
- variable
→ Holds the current item during each iteration.
- in
→ Connects the variable with the sequence.
- sequence
→ The collection being iterated (list, tuple, string, range, etc.).
- statements
→ The code executed in each iteration.
How a for Loop Works
Example:
for number in range(1, 6):
print(number)
Step-by-step execution
Iteration 1
- number
= 1
- Prints
1
Iteration 2
- number
= 2
- Prints
2
Iteration 3
- number
= 3
- Prints
3
Iteration 4
- number
= 4
- Prints
4
Iteration 5
- number
= 5
- Prints
5
The loop ends because there are no more values.
Output
1
2
3
4
5
Flow of a for Loop
Start
│
▼
Read first item
│
▼
Execute code
│
▼
Next item available?
│
Yes ─────────────┐
│ │
▼ │
Execute again ◄───┘
│
No
│
▼
End
Example 1: Print Numbers
for i in range(1, 6):
print(i)
Output
1
2
3
4
5
Imagine a teacher has to call the names of five students.
Without a loop:
print("Aisha")
print("Rahul")
print("Sneha")
print("Rohan")
print("Priya")
This works, but if there are 500 students, writing
print() 500 times would be impractical.
Using a for loop:
students = ["Aisha", "Rahul",
"Sneha", "Rohan", "Priya"]
print(student)
Output
Aisha
Rahul
Sneha
Rohan
Priya
The loop automatically prints every name in the list.
Why Do We Use a for Loop?
A for loop helps us:
- Reduce
repetitive code
- Save
programming time
- Improve
code readability
- Iterate
through data structures
- Perform
calculations repeatedly
- Process
large datasets
- Build
efficient algorithms
Example 2: Print a Message Multiple times
for i in range(5):
print("Welcome to Python")
Output
Welcome to Python
Welcome to Python
Welcome to Python
Welcome to Python
Welcome to Python
Example 3: Loop Through a String
for letter in "Python":
print(letter)
Output
P
y
t
h
o
n
Each iteration processes one character from the string.
Example 4: Loop Through a List
fruits = ["Apple", "Banana",
"Mango", "Orange"]
for fruit in fruits:
print(fruit)
Output
Apple
Banana
Mango
Orange
Example 5: Calculate the Sum of Numbers
total = 0
total = total +
number
Output
Sum = 15
Advantages of for Loop
- Makes
programs shorter.
- Eliminates
repetitive code.
- Easy
to understand.
- Automatically
iterates over sequences.
- Improves
code readability.
- Reduces
programming errors.
- Widely
used in data processing, automation, and machine learning.
Common Mistakes
1. Incorrect Indentation
for i in range(5):
print(i)
This causes an IndentationError because Python
requires the code inside the loop to be indented.
Correct version:
for i in range(5):
print(i)
2. Modifying the Collection While Iterating
Changing a list or dictionary while looping through it can
lead to unexpected results. It is usually better to create a copy or collect
changes separately.
Pattern Programs Using for Loop
Pattern programs are one of the best ways to understand how
loops work because they involve repeating actions in a structured way.
Pattern 1: Square Star Pattern
for i in range(5):
for j in range(5):
print("*", end=" ")
print()
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Explanation
- The
outer loop controls the number of rows (5).
- The
inner loop prints 5 stars in each row.
- print()
moves to the next line after each row.
Pattern 2: Right Triangle
for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print()
Output
*
* *
* * *
* * * *
* * * * *
Explanation
The number of stars increases by one in each row.
Python Variables and Data Types Explained with Examples (Beginner's Guide 2026)
Pattern 3: Inverted Triangle
for i in range(5, 0, -1):
for j in range(i):
print("*", end=" ")
print()
Output
* * * * *
* * * *
* * *
* *
*
Explanation
The number of stars decreases by one in each row.
Pattern 4: Number Triangle
for i in range(1, 6):
for j in range(1,
i + 1):
print(j,
end=" ")
print()
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Explanation
The inner loop prints numbers from 1 to the current row
number.
Pattern 5: Repeated Number Triangle
for i in range(1, 6):
for j in range(i):
print(i,
end=" ")
print()
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Explanation
The current row number is printed repeatedly in each row.
Interview Questions with Answers
1. What is a for loop in Python?
Answer:
A for loop is a control flow statement that
repeatedly executes a block of code for each item in a sequence such as a list,
tuple, string, dictionary, set, or range.
2. Why do we use a for loop?
Answer:
We use a for loop to automate repetitive tasks, reduce code
duplication, iterate through collections, and make programs more efficient and
readable.
3. What is the syntax of a for loop?
Answer:
for variable in sequence:
statements
4. What does the range() function do?
Answer:
The range() function generates a sequence of numbers that is
commonly used with a for loop.
Example:
for i in range(1,6):
print(i)
5. What is a nested for loop?
Answer:
A nested for loop is a for loop placed inside another for
loop. It is commonly used for pattern printing, matrices, and table generation.
6. What is the difference between for and while loops?
Answer:
- for
loop: Used when the number of iterations is known or when iterating
through a sequence.
- while
loop: Used when the number of iterations depends on a condition.
7. What is the purpose of break?
Answer:
The break statement immediately terminates the loop when a
specified condition is met.
8. What does continue do?
Answer:
The continue statement skips the current iteration and moves
to the next iteration of the loop.
9. Can we iterate through a dictionary using a for loop?
Answer:
Yes.
Example:
student = {
"Name":"John",
"Age":20
}
for key, value in student.items():
print(key, value)
10. Can a for loop iterate through a string?
Answer:
Yes.
for letter in "Python":
print(letter)
Each iteration processes one character.
Frequently Asked Questions (FAQs)
1. What is a for loop in Python?
A for loop executes a block of code repeatedly for every
item in a sequence.
2. Why is the for loop important?
It reduces repetitive code, improves readability, and
simplifies data processing.
3. Which data types can be used with a for loop?
A for loop can iterate through:
- Lists
- Tuples
- Strings
- Dictionaries
- Sets
- Range
objects
4. What is range() in Python?
range() creates a sequence of numbers for iteration.
Example:
range(5)
Produces:
0 1 2 3 4
5. Can we stop a for loop before it finishes?
Yes. Use the break statement.
6. What is the difference between break and continue?
- break
exits the loop completely.
- continue
skips only the current iteration.
7. Can we use multiple for loops together?
Yes. This is called a nested for loop.
8. Are for loops used in real-world projects?
Yes. They are widely used in web development, data analysis,
automation, machine learning, artificial intelligence, and scripting.
9. Is a for loop suitable for beginners?
Yes. It is one of the first looping concepts taught in
Python because of its simple syntax and readability.
10. What are common mistakes when using a for loop?
Common mistakes include:
- Incorrect
indentation
- Modifying
a collection while iterating
- Using
the wrong range values
- Forgetting
to use break when needed
- Confusing for and while loops
Summary
The for loop is one of the most important control structures in Python. It allows you to execute a block of code repeatedly for each item in a sequence, reducing repetitive code and making programs more efficient. It is especially useful for working with lists, strings, dictionaries, files, and numerical ranges. Mastering the for loop is essential before moving on to advanced topics such as nested loops, pattern programs, data analysis, and algorithm design.
Comments
Post a Comment