Python for Loop Analysis with Examples

Python for Loop Analysis with Examples

1. What is a for loop in Python?

The for loop in Python is used to iterate over a sequence (like a list, string, tuple, or range) and execute a block of code multiple times — once for each item in the sequence.


2. Syntax of for loop


for item in sequence:
    # block of code

Here, item is a variable that takes each value from the sequence, one by one, and executes the code block inside the loop.


3. for loop with range()

The range() function is commonly used with for loops to repeat code a specific number of times.

Example 1: Print numbers from 0 to 4


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

Output:


0
1
2
3
4
πŸ’‘ Note: range(5) generates numbers from 0 up to (but not including) 5.

Example 2: Using range(start, end)


for i in range(2, 6):
    print(i)

Output:


2
3
4
5
Important: The loop starts at 2 and ends before 6.

4. Iterating over a List


fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Output:


apple
banana
cherry
πŸ’‘ Note: Each loop iteration assigns the next list item to the variable fruit.

5. Iterating over a String


for letter in "Python":
    print(letter)

Output:


P
y
t
h
o
n
Important: Strings are sequences, so you can loop over each character directly.

6. for loop with break statement

Use break to stop the loop early if a condition is met.


for num in range(10):
    if num == 5:
        break
    print(num)

Output:


0
1
2
3
4
⚠️ Caution: When num is 5, the loop exits immediately without printing 5.

7. for loop with continue statement

Use continue to skip the current iteration and move to the next one.


for num in range(5):
    if num == 2:
        continue
    print(num)

Output:


0
1
3
4
πŸ’‘ Note: Number 2 is skipped due to continue.

8. Nested for 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
Important: Each outer loop iteration runs the full inner loop.

9. for loop with else clause


for i in range(3):
    print(i)
else:
    print("Loop completed.")

Output:


0
1
2
Loop completed.
πŸ’‘ Note: The else block runs when the loop ends normally (not interrupted by break).

10. Summary Table

Term Description
for Loop keyword to iterate over sequences
range() Generates a sequence of numbers
break Exits the loop immediately
continue Skips current iteration and continues
else Executes when the loop finishes without break

🎯 Practice writing your own for loops using range(), strings, or lists to understand how iteration works in Python!


πŸ“š Practice Questions:


πŸ“š Related Topics:


πŸ“Œ Bookmark this blog or follow for updates!

Python Logo

πŸ‘ Liked this post? Share it with friends or leave a comment below!

“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python

πŸ“Œ Follow Us And Stay Updated For Daily Updates

Comments