Python for Loop: Purpose, Syntax, and Examples with Lists
1. What is a for loop?
A for loop in Python is used to execute a block of statements..
For example, suppose we have a list containing the names of four students:
students = ["Amit", "Ravi", "Neha", "Priya"]
If we want to print every student’s name, we could write separate print() statements for each name. This becomes inconvenient when the list contains many items.
The for loop makes this easier by taking the items from the list one by one and performing the required operation.
students = ["Amit", "Ravi", "Neha", "Priya"]
for student in students:
print(student)
Output:
Amit Ravi Neha Priya
2. Purpose of the for Loop
The main purpose of a for loop is to execute 1 or more statements in a program. for loop with list are explained in post. .
It is useful when:
- We want to access every item in a list.
- We want to perform the same operation on all list elements.
- We want to display all elements of a list.
- We want to calculate or process information from each element.
For example, consider the following list:
numbers = [10, 20, 30, 40]
for number in numbers:
print(number * 2)
Output:
20 40 60 80
How does it work?
The for loop takes each number from the list
one by one, multiplies it by
2, and displays the result.
numbers = [10, 20, 30, 40]
numbers→ This is the name of the list.[10, 20, 30, 40]→ These are the elements (items) stored in the list.
for number in numbers:
for→ A Python keyword used to start theforloop.number→ This is the loop variable. It takes one item from the list at a time.in→ A Python keyword that tells Python to take items from the list.numbers→ The list from which the items are taken.:→ Indicates that the statements belonging to theforloop are going to follow.
print(number * 2)
number→ Contains the current item taken from the list.*→ Multiplication operator.2→ The value by which the current item is multiplied.print()→ Displays the result on the screen.
So, the important idea is:
for number in numbers
means: take each item from the
numbers
list, one at a time, and store it in
number.
3. Syntax of for Loop
The basic syntax of a Python for loop with a list is:
for variable in list_1:
statement
For example:
fruits = ["Apple", "Mango", "Orange"]
for fruit in fruits:
print(fruit)
In this example:
- for → Python keyword used to start the loop.
- fruit → Loop variable that stores one item from the list at a time.
- in → Python keyword used to take items from the list.
- fruits → The list whose items are processed one by one.
- : → Indicates the beginning of the loop body.
- print(fruit) → Statement executed for each item in the list_1.
Remember: The for loop takes each item from the list one by one and executes the indented statement for each item.
4. How Does the for Loop Work?
Consider the following program:
fruits = ["Apple", "Mango", "Orange"]
for fruit in fruits:
print(fruit)
The for loop takes the items from the fruits list one by one and stores each item in the loop variable fruit.
First iteration
fruit = "Apple" print(fruit)
Output: Apple
Second iteration
fruit = "Mango" print(fruit)
Output: Mango
Third iteration
fruit = "Orange" print(fruit)
Output: Orange
Important: The loop continues until all items in the list have been processed. After the last item is processed, the for loop ends.
5. Indentation in a for Loop
In Python, indentation is used to identify the statements that belong to the for loop.
For example:
students = ["Amit", "Ravi", "Neha"]
for student in students:
print(student)
print("Student name displayed")
Both print() statements are indented, so both statements are executed for every item in the list.
Output:
Amit Student name displayed Ravi Student name displayed Neha Student name displayed
Remember: Statements with the same indentation as the first statement inside the loop belong to the for loop.
A statement without indentation is outside the loop.
students = ["Amit", "Ravi", "Neha"]
for student in students:
print(student)
print("Loop completed")
Here, the last print() statement is outside the loop, so it executes only once after the loop has finished.
Output:
Amit Ravi Neha Loop completed
Key Point: In Python, indentation is not just for appearance. It tells Python which statements belong to the loop.
A Simple Way to Remember
A
forloop takes items from a list one by one and executes the loop body for each item.
Practice Programs with Different List Objects
A list can contain different types of objects such as Integer, Float, String and Boolean objects. Try the following simple programs.
1. Integer List
Print each integer.
numbers = [10, 20, 30]
for number in numbers:
print(number)
Output: 10 20 30
2. Integer Calculation
Find the square of each number.
numbers = [2, 4, 6]
for number in numbers:
print(number * number)
Output: 4 16 36
3. Float List
Print each float.
values = [1.5, 2.5, 3.5]
for value in values:
print(value)
Output: 1.5 2.5 3.5
4. Float Calculation
Add 1 to each float.
values = [1.5, 2.5, 3.5]
for value in values:
print(value + 1)
Output: 2.5 3.5 4.5
5. String List
Print each string.
names = ["Amit", "Ravi", "Neha"]
for name in names:
print(name)
Output: Amit Ravi Neha
6. String Operation
Print each name in uppercase.
names = ["amit", "ravi", "neha"]
for name in names:
print(name.upper())
Output: AMIT RAVI NEHA
7. Boolean List
Print each Boolean value.
values = [True, False, True]
for value in values:
print(value)
Output: True False True
8. Mixed List
Print different objects.
items = [10, 2.5, "Python", True]
for item in items:
print(item)
Output: 10 2.5 Python True
Key Point: The for loop can process each object in a list, regardless of whether the object is an integer, float, string or Boolean.