Python for Loop
The for loop in Python is used to repeat a block of code for
each item in a sequence or other iterable object. It is commonly used
with lists, tuples, strings, dictionaries, sets and the
range() function.
Unlike a traditional counter-controlled loop, a Python
for loop directly accesses each item of the iterable.
Syntax of for Loop
for variable in iterable:
statement
The loop variable takes each value from the iterable one by one and executes the indented statements for every value.
Simple for Loop
for i in range(5):
print(i)
0 1 2 3 4
The range(5) function generates values from
0 to 4.
for Loop with a List
fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)
Apple Banana Mango
The loop accesses each element of the list one at a time.
for Loop with a Tuple
numbers = (10, 20, 30, 40)
for number in numbers:
print(number)
10 20 30 40
for Loop with a String
A string is an iterable sequence of characters. Therefore, a
for loop can be used to process each character separately.
word = "Python"
for ch in word:
print(ch)
P y t h o n
for Loop with range()
for i in range(1, 6):
print(i)
1 2 3 4 5
Here, the starting value is 1 and the stopping value is
6. The stopping value is not included.
for Loop with Start and Stop Values
for i in range(5, 10):
print(i)
5 6 7 8 9
for Loop with Step Value
for i in range(2, 11, 2):
print(i)
2 4 6 8 10
The third argument of range() specifies the step size.
Here, the value increases by 2 after every iteration.
Reverse for Loop
for i in range(5, 0, -1):
print(i)
5 4 3 2 1
Printing Even Numbers
for i in range(2, 11, 2):
print(i)
2 4 6 8 10
Printing Odd Numbers
for i in range(1, 11, 2):
print(i)
1 3 5 7 9
Calculating Sum using for Loop
numbers = [10, 20, 30, 40, 50]
total = 0
for number in numbers:
total = total + number
print("Sum =", total)
Sum = 150
Finding the Largest Number
numbers = [12, 45, 23, 67, 34]
largest = numbers[0]
for number in numbers:
if number > largest:
largest = number
print("Largest =", largest)
Largest = 67
Counting Elements using for Loop
numbers = [10, 20, 30, 40, 50]
count = 0
for number in numbers:
count = count + 1
print("Number of elements =", count)
Number of elements = 5
for Loop with if Statement
A for loop can contain an if statement to
perform conditional processing during each iteration.
numbers = [10, 15, 20, 25, 30]
for number in numbers:
if number % 2 == 0:
print(number)
10 20 30
for Loop with else
Python allows an else block to be associated with a
for loop. The else block executes when the
loop completes normally.
for i in range(5):
print(i)
else:
print("Loop completed")
0 1 2 3 4 Loop completed
Nested for Loop
A nested for loop is a for loop inside another
for loop.
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
Multiplication Table using for Loop
number = 5
for i in range(1, 11):
print(number, "x", i, "=", number * i)
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Using break in for Loop
The break statement terminates the loop immediately.
for i in range(1, 10):
if i == 5:
break
print(i)
1 2 3 4
Using continue in for Loop
The continue statement skips the current iteration and
moves to the next iteration.
for i in range(1, 6):
if i == 3:
continue
print(i)
1 2 4 5
Using pass in for Loop
The pass statement does nothing. It can be used as a
placeholder where a statement is syntactically required.
for i in range(5):
if i == 2:
pass
print(i)
0 1 2 3 4
Practical Example: Factorial using for Loop
number = 5
factorial = 1
for i in range(1, number + 1):
factorial = factorial * i
print("Factorial =", factorial)
Factorial = 120
Practical Example: Count Vowels
text = "Python Programming"
count = 0
for ch in text.lower():
if ch in "aeiou":
count = count + 1
print("Number of vowels =", count)
Number of vowels = 4
Practical Example: Reverse a String
text = "Python"
reverse_text = ""
for ch in text:
reverse_text = ch + reverse_text
print(reverse_text)
nohtyP
Practical Example: Pattern using for Loop
for i in range(1, 6):
print("*" * i)
* ** *** **** *****
for Loop with Dictionary
student = {
"name": "Rahul",
"age": 20,
"marks": 85
}
for key in student:
print(key, ":", student[key])
name : Rahul age : 20 marks : 85
for Loop with Dictionary Items
student = {
"name": "Rahul",
"marks": 85
}
for key, value in student.items():
print(key, ":", value)
name : Rahul marks : 85
for Loop with Set
numbers = {10, 20, 30}
for number in numbers:
print(number)
A set is unordered, so the order in which its elements are displayed should not be assumed.
Important Points
- The
forloop is used to iterate over an iterable. - Lists, tuples, strings, dictionaries, sets and ranges can be used with a
forloop. - The loop variable receives one item at a time.
- Indentation defines the body of the loop.
- The
range()function is commonly used withforloops. breakterminates the loop.continueskips the current iteration.passacts as a placeholder.- A
forloop can contain anifstatement. - Python also supports nested
forloops.
for loop repeatedly executes a block of code
for each item in an iterable. It is one of the most commonly used
looping statements in Python.