Lesson 21 of 70 – Python For Loop
30%

Python For Loop

A for loop is used to repeat a block of code for each item in a sequence such as a list, tuple, string, or range of numbers.

Note: The for loop is commonly used when we know what items or range of values we want to process.
What is a For Loop?

A for loop executes a block of code once for every item in a sequence.

for variable in sequence:
    statement

Here, the variable receives each item from the sequence one by one.

Simple For Loop
for i in range(5):
    print(i)
Output:
0
1
2
3
4

The range(5) generates numbers from 0 to 4.

For Loop with List
names = ["Rahul", "Amit", "Priya"]

for name in names:
    print(name)
Output:
Rahul
Amit
Priya
For Loop with String
name = "Python"

for letter in name:
    print(letter)
Output:
P
y
t
h
o
n

A string is a sequence of characters, so the for loop can process each character separately.

For Loop with range()

The range() function is frequently used with for loops.

for i in range(1, 6):
    print(i)
Output:
1
2
3
4
5
range() with Start and Stop
for i in range(2, 7):
    print(i)
Output:
2
3
4
5
6

The first value is included, but the second value is not included.

range() with Step
for i in range(1, 11, 2):
    print(i)
Output:
1
3
5
7
9

The third argument specifies the step value.

Printing Even Numbers
for i in range(2, 11, 2):
    print(i)
Output:
2
4
6
8
10
Printing Odd Numbers
for i in range(1, 10, 2):
    print(i)
Output:
1
3
5
7
9
For Loop with Tuple
numbers = (10, 20, 30, 40)

for number in numbers:
    print(number)
Output:
10
20
30
40
For Loop with Set
numbers = {10, 20, 30}

for number in numbers:
    print(number)

A for loop can iterate through the elements of a set. The order of set elements should not be relied upon.

For Loop with Dictionary
student = {
    "name": "Rahul",
    "age": 20
}

for key in student:
    print(key)
Output:
name
age
Loop Through Dictionary Values
student = {
    "name": "Rahul",
    "age": 20
}

for value in student.values():
    print(value)
Output:
Rahul
20
Loop Through Dictionary Keys and Values
student = {
    "name": "Rahul",
    "age": 20
}

for key, value in student.items():
    print(key, value)
Output:
name Rahul
age 20
For Loop with Condition
for i in range(1, 11):

    if i % 2 == 0:
        print(i)
Output:
2
4
6
8
10
Calculate Sum Using For Loop
total = 0

for i in range(1, 6):
    total = total + i

print(total)
Output:
15

The loop adds the numbers from 1 to 5.

Multiplication Table
number = 5

for i in range(1, 11):
    print(number * i)
Output:
5
10
15
20
25
30
35
40
45
50
For Loop with else

Python also allows an else block with a for loop.

for i in range(5):
    print(i)
else:
    print("Loop completed")
Output:
0
1
2
3
4
Loop completed
Nested For Loop

A for loop can also be placed inside another for loop. This is called a nested for loop.

for i in range(1, 4):

    for j in range(1, 3):
        print(i, j)
Output:
1 1
1 2
2 1
2 2
3 1
3 2
For Loop with User Input
numbers = input("Enter numbers: ")

for number in numbers:
    print(number)

If the user enters:

12345

The output will be:

1
2
3
4
5
For Loop with List of Names
students = ["Amit", "Rahul", "Priya", "Neha"]

for student in students:
    print("Welcome", student)
Output:
Welcome Amit
Welcome Rahul
Welcome Priya
Welcome Neha
Using len() with For Loop
names = ["Amit", "Rahul", "Priya"]

for i in range(len(names)):
    print(names[i])
Output:
Amit
Rahul
Priya

Here, len() returns the number of elements in the list.

Using enumerate()

The enumerate() function can be used when we need both the index and the value.

names = ["Amit", "Rahul", "Priya"]

for index, name in enumerate(names):
    print(index, name)
Output:
0 Amit
1 Rahul
2 Priya
For Loop with Reverse Range
for i in range(5, 0, -1):
    print(i)
Output:
5
4
3
2
1

A negative step allows the loop to move backwards.

Important: Indentation in For Loop

The statements inside a for loop must be indented.

for i in range(3):
    print(i)

The print() statement belongs to the loop because it is indented.

Incorrect:
for i in range(3):
print(i)
This can cause an indentation error.
Key Points
  • The for loop repeats code for each item in a sequence.
  • It can work with lists, tuples, strings, sets, dictionaries, and ranges.
  • The range() function is commonly used with for loops.
  • The start value of range is included.
  • The stop value of range is excluded.
  • The step value controls how the sequence changes.
  • A for loop can contain conditions.
  • A for loop can have an else block.
  • For loops can be nested.
  • Proper indentation is required.

🧠 Quick Quiz

Question: Which keyword is used to create a for loop in Python?