Understanding Python for Loops: A Beginner’s Guide | Fresh Spar Technologies | Manojkumar Chandrasekar
When you first dive into Python, one of the essential concepts you’ll encounter is the for
loop. It's a powerful tool that allows you to repeat tasks over a collection of items, whether you're iterating through a list, a range of numbers, or even the characters of a string. In this blog, we'll explore how for
loops work and why they are a must-know for any Python developer.
What is a for
Loop?
A for
loop in Python lets you run a block of code repeatedly for every item in a sequence. Sequences can be lists, tuples, strings, or ranges. Instead of writing repetitive code, a for
loop automates the process by handling each element one at a time.
Basic Syntax:
pythonCopy codefor item in sequence:
# Code to execute
item
: The variable that will take on each value in the sequence.sequence
: The collection you are looping over, such as a list, string, or range.
Example 1: Looping Through a List
Let’s say we have a list of fruits, and we want to print each one. Instead of writing print()
for each item, we can use a for
loop:
pythonCopy codefruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
Output:
Copy codeapple
banana
cherry
This simple loop goes through each fruit in the list and prints it out. Easy, right?
Example 2: Using range()
If you need to loop a specific number of times, the range()
function is your friend. It generates a sequence of numbers that you can iterate over:
pythonCopy codefor i in range(5):
print(i)
Output:
Copy code0
1
2
3
4
Here, the range(5)
function gives us numbers from 0 to 4 (not including 5), and the loop prints each number.
Example 3: Iterating Through a String
You can also use for
loops to iterate through the characters of a string. For example:
pythonCopy codeword = "Python"
for letter in word:
print(letter)
Output:
cssCopy codeP
y
t
h
o
n
The loop goes through each character in the string “Python” and prints it on a new line.
for
Loop with else
Did you know that you can use an else
block with a for
loop? It will run after the loop finishes, unless the loop is stopped with a break
statement.
pythonCopy codefor i in range(3):
print(i)
else:
print("Loop completed!")
Output:
vbnetCopy code0
1
2
Loop completed!
Controlling the Loop: break
and continue
break
: Terminates the loop entirely.continue
: Skips the current iteration and moves to the next one.
Example with break
:
pythonCopy codefor i in range(5):
if i == 3:
break
print(i)
Output:
Copy code0
1
2
Example with continue
:
pythonCopy codefor i in range(5):
if i == 3:
continue
print(i)
Output:
Copy code0
1
2
4
As you can see, break
stops the loop when i
equals 3, and continue
skips the current iteration, moving directly to the next one.
Conclusion
Python’s for
loops are a simple yet powerful tool that saves time by automating repetitive tasks. Whether you're processing a list, counting through a range, or even breaking out of loops early, for
loops offer flexibility and control.
Mastering loops is a key step in your Python journey, so take the time to experiment and get comfortable with them. Before you know it, you’ll be using them to tackle more complex challenges in your code!
Subscribe to my newsletter
Read articles from Manojkumar Chandrasekar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by