How does a for loop iterate over a list in Python?

QuestionsQuestions8 SkillsProPython Control StructuresAug, 22 2025
0119

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

  • numbers is a list containing integers from 1 to 5.
  • The loop for number in numbers: iterates over each element in the numbers list.
  • Inside the loop, print(number) prints each value of number one 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.

0 Comments

no data
Be the first to share your comment!