Loops are the central part of programming.
You can repeat actions automatically with the help of code written inside a for loop.
It provides 3 main benefits: -
a) saves your time.
b) reduces the number of errors.
c) helps you to focus on real-time problems.
In Python language, the for loop is a very critical tool that every programmer uses on a daily basis.
It helps in multiple scenarios like analyzing data, building AI models, and processing user input.
In this chapter, I will explain everything from basic level to real-world use cases with simple explanations (till professional level).
1. What is a for loop?
It is a structure in Python language which is used to repeat a block of code again and again.
Basic Syntax
for variable in sequence:
# block of codea) for keyword: - It tells Python that you want to loop.
b) variable: - It stores each item in the sequence one by one.
c) sequence: - It can be a list, string, tuple, range etc. In simple words, it is a collection of data that you want to loop through.
d) block of code: - It contains a list of tasks written in the form of Python code which you want to repeat.
For Loop Flowchart:

Flowchart Explanation:
1. Start
This is where the loop begins. The program is ready to start going through each item in the sequence.
2. Last Item? (Decision Box)
Here the program checks:
“Do I still have more items left to process in the sequence?”
If there are items remaining, it follows the “No” arrow and continues.
If all items are finished, it follows the “Yes” arrow to End.
3. Statement
If items are still left, the program executes the code written inside the loop.
This could be printing values, doing calculations, or any operation you placed in the loop body.
4. Try Next Number / Next Item
After running the statement, the program automatically moves to the next item in the sequence and loops back to check again if more items are left.
5. End
When all items in the sequence are processed, the loop stops. The program exits the loop and continues forward.
Example 1:
numbers = [5, 10, 15, 20]
for n in numbers:
print(n)Output:
5
10
15
20
Explanation:
In this example, we have a simple list of numbers: 5, 10, 15, and 20.
The for loop goes through the list one number at a time. On every round, Python picks the next number and prints it.
So the loop first prints 5, then 10, then 15, and finally 20.
Example 2:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("I love", fruit)Output:
I love apple
I love banana
I love cherry
Explanation:
In this example, we have a small list of fruits that most of us eat every day.
When the loop runs, Python takes one fruit at a time from the list and plugs it into the sentence “I love …”
So the program prints the output (as shown below):
• I love apple
• I love banana
• I love cherry
Real World Applications:
a) It is used to show all products on an e-commerce website.
b) It is also used to analyze rows of data from an Excel file.
c) You can also use it to send bulk notifications or emails.
Why It Matters:
It helps you automate a particular task. For example, instead of writing the same printing statements 100 times, you can write a single for loop. It will make your code smart and clean.
2. The range() function
It generates a sequence of numbers for looping. You can also mention start, stop, and step values.
It will generate numbers from start till stop-1 automatically.
Example 1:
for i in range(5):
print(i)
Output:
0
1
2
3
4
You can also customize it (as shown below):
Example 2:
for i in range (2, 10, 2):
print(i)Output:
2
4
6
8
Explanation:
In this example, the range(2, 10, 2) function generates numbers starting from 2, going up to 10 (but not including 10), and increasing by 2 each time.
So the loop prints: 2, 4, 6, 8 - all the even numbers in that range.
It’s a simple way to control where a loop starts, where it stops, and how much it moves forward in each step.
Real World Applications:
a) It is used to generate invoice numbers.
b) It is also used to process the dataset into small batches in ML training.
Why It Matters:
The range() function gives you full control over the iteration process.
3. The += Operator
It is also known as the “Augmented Assignment Operator”.
Basically, +=operator performs three tasks at once (as mentioned below):
a) Takes the current value of a variable
b) Adds the new value to it
c) Finally, stores the final result into the same variable.
Example:
total = 10
total += 5
print(total)Output:
15
In this example, we start with a variable named total that holds the value 10.
The line total += 5 is a shortcut that means “take the current value of total and add 5 to it.”
So, Python picks the existing value (10), adds 5, and updates total to 15.
Why It Matters:
It helps to make your code smart and professional.
4. The enumerate () function
It allows you to access a list item and its position at the same time.
In simple words, enumerate () function helps you in 2 ways: -
a) gives you the index of each item automatically
b) also gives you the value of that item at the same time
Example:
fruits = ["apple", "banana", "orange"]
for index, fruit in enumerate(fruits):
print(index, fruit)Output:
0 apple
1 banana
2 orange
Explanation:
In this example, we have a list of fruits.
When we use enumerate(fruits) function, Python gives us two things at the same time in every loop:
1. the position number (index)
2. the actual fruit name
So, the loop goes like this -
first it prints 0 apple, then 1 banana, and finally 2 orange.
In simple words, It is an easy way to get both the item and its position without writing extra code.
Real World Application:
a) It helps you print serial numbers 1, 2, 3… automatically in report generation.
b) You can also use it to attach row numbers to each entry while exporting data in a CSV file.
5. The zip () function
This function helps you combine multiple sequences and iterate through them together.
Example:
names = ["Aman", "Rajesh", "Monika"]
scores = [85, 90, 78]
for name, score in zip (names, scores):
print(f"{name} scored {score}")Output:Aman scored 85
Rajesh scored 90
Monika scored 78
Explanation:
In this example, we have two separate lists - one contains student names and the other contains their scores.
The zip () function pairs the first name with the first score, the second name with the second score, and so on.
So, when the loop runs:
• Aman gets paired with 85
• Rajesh gets paired with 90
• Monika gets paired with 78
The loop then prints each pair in a clean, readable format like:
Real World Applications:
a) It is used to combine columns of data from multiple sources.
b) In games, x and y coordinates are stored in different arrays. The zip () function joins them to form (x,y) points for drawing and movement purposes.
6. Short Way to Write Loops – List Comprehension:
List Comprehension allows you to write loops in a single line and in a compact way.
It makes the code shorter and faster.
Example:
numbers = [1, 2, 3, 4, 5]
squares = [n*n for n in numbers]
print(squares)Output:
[1, 4, 9, 16, 25]
In this example, we take a simple list of numbers and create a new list that contains the square of each number.
The line squares = [n*n for n in numbers] is a list comprehension, which means Python does the entire loop in one clean, compact line.
So, Python picks each number from the list, squares it, and stores all the results in a new list called squares.
Why It Matters:
It helps you write your code in a concise and highly efficient way.
Learn more about Loop in our “Python for Loop Advanced Concepts” chapter.