Learn how to use the for loop in Python with practical examples
The for loop is a fundamental concept in programming that allows you to iterate over a sequence of elements. In Python, the for loop is used to execute a block of code repeatedly for each item in a sequence, such as a list, tuple, or string. This article will guide you through the basics of using the for loop in Python with practical examples.
Syntax of the for loop:
The syntax of the for loop in Python is as follows:
“`
for item in sequence:
# code block to be executed
“`
The `item` variable represents the current element being iterated over, and the `sequence` is the collection of elements to iterate through. The code block following the colon is indented and will be executed for each item in the sequence.
Example 1: Iterating over a list
Let’s start with a simple example of iterating over a list using a for loop:
“`python
fruits = [“apple”, “banana”, “orange”]
for fruit in fruits:
print(fruit)
“`
Output:
“`
apple
banana
orange
“`
In this example, we have a list of fruits, and the for loop iterates over each fruit in the list. The `fruit` variable takes on the value of each item in the list, and we print it out.
Example 2: Iterating over a string
The for loop can also be used to iterate over a string. Each character in the string is treated as an individual item in the sequence:
“`python
message = “Hello, World!”
for char in message:
print(char)
“`
Output:
“`
H
e
l
l
o
,
W
o
r
l
d
!
“`
In this example, we iterate over each character in the string `message` and print it out.
Example 3: Using the range() function
The `range()` function is often used in conjunction with the for loop to generate a sequence of numbers. It takes in three arguments: `start`, `stop`, and `step`. The `start` argument specifies the starting value of the sequence (default is 0), the `stop` argument specifies the ending value (exclusive), and the `step` argument specifies the increment (default is 1).
“`python
for num in range(1, 10, 2):
print(num)
“`
Output:
“`
1
3
5
7
9
“`
In this example, we use the `range()` function to generate a sequence of odd numbers from 1 to 10 (exclusive). The for loop iterates over each number in the sequence and prints it out.
Example 4: Nested for loops
You can also have nested for loops, where one loop is inside another. This is useful when you need to iterate over multiple sequences simultaneously:
“`python
colors = [“red”, “green”, “blue”]
fruits = [“apple”, “banana”, “orange”]
for color in colors:
for fruit in fruits:
print(color, fruit)
“`
Output:
“`
red apple
red banana
red orange
green apple
green banana
green orange
blue apple
blue banana
blue orange
“`
In this example, we have two lists, `colors` and `fruits`. The outer for loop iterates over each color, and the inner for loop iterates over each fruit. We print out the combination of color and fruit.
Conclusion:
The for loop is a powerful tool in Python for iterating over sequences. It allows you to perform repetitive tasks efficiently and effectively. By understanding the syntax and examples provided in this article, you should now have a solid foundation for using the for loop in your Python programs.
- SEO Powered Content & PR Distribution. Get Amplified Today.
- PlatoData.Network Vertical Generative Ai. Empower Yourself. Access Here.
- PlatoAiStream. Web3 Intelligence. Knowledge Amplified. Access Here.
- PlatoESG. Automotive / EVs, Carbon, CleanTech, Energy, Environment, Solar, Waste Management. Access Here.
- BlockOffsets. Modernizing Environmental Offset Ownership. Access Here.
- Source: Plato Data Intelligence.