1.
for Loop (Iterating over a list):
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
nginx
CopyEdit
apple
banana
cherry
2. for Loop (Using range()):
for i in range(5):
print(i)
Output:
0
1
2
3
4
3. while Loop (With a Counter):
count = 0
while count < 5:
print(count)
count += 1
Output:
0
1
2
3
4
4. Loop with break (Stopping Early):
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
5. Loop with continue (Skipping Iterations):
for i in range(5):
if i == 3:
continue
print(i)
Output:
0
1
2
4
6. Nested Loops:
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
Output:
i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
i=2, j=0
i=2, j=1
The for loop with the range() function is commonly used in Python to iterate over a sequence of
numbers. Here's a detailed explanation:
Syntax of range()
The range() function generates a sequence of numbers, which is then iterated over by the for
loop. It can take one, two, or three arguments:
1. range(stop)
Generates numbers from 0 up to (but not including) stop.
2. range(start, stop)
Generates numbers from start up to (but not including) stop.
3. range(start, stop, step)
Generates numbers from start to stop, incrementing by step.
Examples
1. Basic Example (range(stop)):
for i in range(5):
print(i)
Explanation:
o Generates numbers: 0, 1, 2, 3, 4 (from 0 to stop-1).
o The loop runs 5 times, printing each number.
Output:
0
1
2
3
4
2. Using start and stop (range(start, stop)):
for i in range(2, 6):
print(i)
Explanation:
o Generates numbers: 2, 3, 4, 5 (from start to stop-1).
Output:
2
3
4
5
3. Using step (range(start, stop, step)):
for i in range(0, 10, 2):
print(i)
Explanation:
o Generates numbers: 0, 2, 4, 6, 8 (from start to stop-1, incrementing by 2).
Output:
0
2
4
6
8
4. Using Negative step:
for i in range(5, 0, -1):
print(i)
Explanation:
o Generates numbers: 5, 4, 3, 2, 1 (from start to stop+1, decrementing by -
1).
Output:
5
4
3
2
1
Key Points
• The range does not include the stop value.
• The step can be positive (incrementing) or negative (decrementing).
• Useful for creating controlled loops without explicitly maintaining a counter variable.
1. Print Numbers from 0 to 9
python
CopyEdit
for i in range(10):
print(i)
Output:
0
1
2
3
4
5
6
7
8
9
2. Print Numbers from 1 to 5
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
3. Print Even Numbers from 0 to 10
for i in range(0, 11, 2):
print(i)
Output:
0
2
4
6
8
10
4. Print Numbers in Reverse Order
for i in range(10, 0, -1):
print(i)
Output:
10
9
8
7
6
5
4
3
2
1
5. Sum of Numbers from 1 to 10
total = 0
for i in range(1, 11):
total += i
print("Sum:", total)
Output:
Sum: 55
6. Print Multiplication Table of 3
for i in range(1, 11):
print(f"3 x {i} = {3 * i}")
Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
7. Print Square of Each Number
for i in range(1, 6):
print(f"The square of {i} is {i ** 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
8. Nested Loops Example
for i in range(1, 4):
for j in range(1, 4):
print(f"i={i}, j={j}")
Output:
i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=2, j=2
i=2, j=3
i=3, j=1
i=3, j=2
i=3, j=3
9. Find Odd Numbers Between 1 and 10
for i in range(1, 11):
if i % 2 != 0:
print(i)
Output:
1
3
5
7
9
10. Loop Through Characters of a String with Index
text = "Hello"
for i in range(len(text)):
print(f"Character at index {i} is {text[i]}")
Output:
Character at index 0 is H
Character at index 1 is e
Character at index 2 is l
Character at index 3 is l
Character at index 4 is o