A loop is a programming construct that repeats a block of code multiple times, without requiring the programmer to write the same instruction over and over. Python provides two types of loops: the for loop and the while loop. This lesson covers the for loop, which is used when you know in advance how many times you want a block of code to run, or when you want to go through every item in a sequence such as a string, list, or range of numbers.
1. Definition and Syntax
A for loop is used to iterate over a sequence, such as a string, list, tuple, or range of numbers, executing the indented block of code once for each item in that sequence.
for variable in sequence:
# code to execute for each itemHere, variable takes on the value of each item in sequence, one at a time, until every item has been processed.
2. Looping Through a Range of Numbers
The most common use of a for loop for beginners is combined with the built-in range() function, which generates a sequence of numbers.
for i in range(5):
print(i)Output:
0
1
2
3
4Important: range(5) produces the numbers 0, 1, 2, 3, 4, five numbers in total, but stops before reaching 5. This is a common point of confusion, range(5) does not include 5 itself.
range() can also take a starting point and a step value:
for i in range(2, 10, 2):
print(i)Output:
2
4
6
8Here, range(2, 10, 2) starts at 2, stops before 10, and increases by 2 each time.
3. Looping Through a String
Since a string is a sequence of characters, a for loop can go through each character one at a time.
name = "Nepal"
for letter in name:
print(letter)Output:
N
e
p
a
l4. Looping Through a List
A for loop is also the standard way to process every item in a list, a data type covered in more detail in an upcoming lesson.
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)Output:
apple
banana
mango5. Using break and continue
The break statement immediately stops the loop entirely, skipping any remaining iterations. The continue statement skips only the current iteration and moves on to the next one, without stopping the loop.
for i in range(10):
if i == 5:
break
print(i)Output:
0
1
2
3
4The loop stops completely the moment i reaches 5, so 5, 6, 7, 8, 9 are never printed.
for i in range(10):
if i % 2 == 0:
continue
print(i)Output:
1
3
5
7
9Here, continue skips the print statement whenever i is even, but the loop itself keeps running through every number up to 9.
6. The else Clause with a for Loop
Python allows an optional else block after a for loop. This block executes only if the loop completes fully without being interrupted by a break statement.
for i in range(5):
print(i)
else:
print("Loop finished without a break.")If a break statement had stopped the loop early, the else block would not run at all. This feature is used far less often than the loop itself, but it does occasionally appear in exam questions.
Practice Questions
1. Write a for loop that prints numbers from 1 to 10.
2. Write a for loop that prints all even numbers between 1 and 20 using range() with a step value.
3. What is the output of the following code?
for i in range(3):
print(i * 2)4. Write a program that loops through the string "Python" and prints each character on a separate line.
5. Write a for loop that stops as soon as it encounters the number 7 while looping through range(1, 15).
6. What is the difference in output between using break and continue when the loop hits i == 3 in range(6)? Write both versions and compare.
