A for loop in Python iterates over a list by taking each element of the list one at a time and executing a block of code for each element. Here's the basic syntax:
for <variable> in <sequence>:
<statements>
Example
Here's a simple example of how a for loop iterates over a list of integers:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
Explanation
numbersis a list containing integers from 1 to 5.- The loop
for number in numbers:iterates over each element in thenumberslist. - Inside the loop,
print(number)prints each value ofnumberone by one.
When you run this code, the output will be:
1
2
3
4
5
This demonstrates how the for loop processes each item in the list sequentially.
