The For Loop in Python
Python offers several constructs to handle repetitive tasks efficiently. One such very important and must-know construct is the 'for' loop.
In this module, we will explore the 'for' loop in Python, its syntax, implementation, and discuss some code examples to illustrate its functionality.
Whether you're a beginner or an experienced programmer, understanding the 'for' loop will undoubtedly enhance your ability to write clean and concise code.
What is a For Loop?
A 'for' loop is a control flow statement that allows you to iterate over a sequence of elements. It executes a block of code repeatedly, once for each item in the sequence, until all the elements have been processed.
The 'for' loop is particularly useful when you know the number of iterations in advance or when you need to iterate over a fixed-length sequence, such as a list or a string.
Syntax: The basic syntax of a 'for' loop in Python is as follows:
>>> for item in sequence:
>>> # Code block to be executed
The 'item' represents the current element in the sequence, and 'sequence' refers to the iterable object over which the loop iterates.
The code block , indented below the 'for' statement, defines the actions to be performed for each item in the sequence.
Iterating over a List
>>> fruits = ['apple', 'banana', 'cherry']
>>> for fruit in fruits:
>>> print(fruit)
apple
banana
cherry
- In this code example, the 'for' loop iterates over each element in the 'fruits' list.
- For each iteration, the current item is assigned to the variable 'fruit', and the print statement displays the value of 'fruit'.
- As a result, the loop prints each fruit on a separate line.
Iterating over a String
>>> message = "Hello, World!"
>>> for char in message:
>>> print(char)
H
e
l
l
o
W
o
r
l
d
- Here, the 'for' loop iterates over each character in the 'message' string.
- For every iteration, the current character is assigned to the variable 'char', which is then printed.
- Thus, the loop prints each character of the string on a separate line.
Using the range() Function
>>> for i in range(1, 6):
>>> print(i)
1
2
3
4
5
- In this above code, the 'range()' function generates a sequence of numbers from 1 to 5 (excluding 6).
- The 'for' loop iterates over each number in the range, and the value of 'i' is printed.
- As a result, the loop displays the numbers 1 to 5.
Nested For Loops
>>> shapes = ['circle', 'square', 'triangle']
>>> colors = ['red', 'blue', 'green']
>>> for shape in shapes:
>>> for color in colors:
>>> print(shape, color)
circle red
circle blue
circle green
square red
square blue
square green
triangle red
triangle blue
triangle green
- This example demonstrates nested 'for' loops, where the outer loop iterates over the 'shapes' list, and the inner loop iterates over the 'colors' list.
- The combination of each shape and color is printed during each iteration, resulting in a Cartesian product of the two lists.
Module Takeaways…
The 'for' loop is an invaluable tool in Python for handling repetitive tasks. It simplifies the process of iterating over a sequence of elements and executing specific actions for each item.
With the knowledge gained from this article, you can now effectively use the 'for' loop to streamline your code and achieve greater efficiency in your programming endeavors.
Now that we've mastered all the basics of for loops in Python, its time to conquer another important topic on our journey to mastering Python: the Ternary Operator. See you in the next module!