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:
- ➤ Python Arithmetic Operators
- ➤ Basic String Functions
- ➤ Advanced String Functions
- ➤ Basic List Functions
- ➤ Advanced List Functions : Part-1
- ➤ Advanced List Functions : Part-2
- ➤ Basic Tuple Functions
- ➤ Advanced Tuple Functions
- ➤ Basic Dictionary Functions
- ➤ Advanced Dictionary Functions
- ➤ Conditional Statements : if-elif-else
- ➤ Python 'for' Loop
- ➤ Python 'while' Loop
- ➤ Difference between 'for' loop and 'while' loop
- ➤ Introducing Python Functions
π Bookmark this blog or follow for updates!
π Liked this post? Share it with friends or leave a comment below!
πTrending Topics
π Connect With Us:
“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
Post a Comment