Introduction
The for
loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a block of code for each item in the sequence. The for
loop is one of the most commonly used looping constructs in Python due to its simplicity and flexibility.
Syntax
for variable in sequence:
# block of code
Example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
Using break in a for Loop
The break
statement is used to exit the loop prematurely, regardless of the loop condition.
Example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
if fruit == "banana":
break
print(fruit)
Output:
apple
Using continue in a for Loop
The continue
statement skips the current iteration and proceeds to the next iteration of the loop.
Example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
if fruit == "banana":
continue
print(fruit)
Output:
apple
cherry
Iterating Over a String
A for
loop can be used to iterate over each character in a string.
Example
text = "Hello"
for char in text:
print(char)
Output:
H
e
l
l
o
The range() Function
The range()
function generates a sequence of numbers, which is often used with the for
loop to execute a block of code a specific number of times. The range()
function has three different ways to be called:
range(stop)
: Generates numbers from0
tostop - 1
.range(start, stop)
: Generates numbers fromstart
tostop - 1
.range(start, stop, step)
: Generates numbers fromstart
tostop - 1
, with a specified step.
Examples
-
Using
range(stop)
:for i in range(5): print(i)
Output:
0 1 2 3 4
-
Using
range(start, stop)
:for i in range(2, 7): print(i)
Output:
2 3 4 5 6
-
Using
range(start, stop, step)
:for i in range(1, 10, 2): print(i)
Output:
1 3 5 7 9
Simple Programs Using for Loop
Program 1: Sum of First N Natural Numbers
This program calculates the sum of the first n
natural numbers.
n = 10
sum = 0
for i in range(1, n + 1):
sum += i
print(f"The sum of the first {n} natural numbers is {sum}")
Output:
The sum of the first 10 natural numbers is 55
Program 2: Factorial of a Number
This program calculates the factorial of a given number.
num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}")
Output:
The factorial of 5 is 120
Program 3: Print Each Character of a String
This program prints each character of a given string.
text = "Hello, World!"
for char in text:
print(char)
Output:
H
e
l
l
o
,
W
o
r
l
d
!
Program 4: Iterate Over a List of Numbers and Print Their Squares
This program iterates over a list of numbers and prints their squares.
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(f"The square of {num} is {num**2}")
Output:
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
Program 5: Print Even Numbers from a List
This program prints all the even numbers from a given list.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num % 2 == 0:
print(f"{num} is an even number")
Output:
2 is an even number
4 is an even number
6 is an even number
8 is an even number
10 is an even number
Conclusion
The for
loop in Python is used for iterating over sequences and performing repetitive tasks efficiently. By understanding how to use the for
loop, along with the range()
function and control statements like break
and continue
, you can write more flexible and efficient code. The provided examples and programs demonstrate practical applications of the for
loop in solving common programming tasks.