Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

for

The for statement repeats a suite for each element produced by a sequence or other iterable. It binds each element to a target and supports break, continue, and an optional else clause.

How it works

A for statement obtains an iterator from its iterable and assigns each yielded value to the target before running the indented suite. It can include else, which runs if iteration finishes normally rather than through break.

Syntax

for target_list in expression_list:
    suite
[else:
    suite]

Key points

  • Use for to process values from sequences and other iterable objects.
  • The loop works with lists, tuples, strings, dictionaries, sets, generators, and custom iterables.
  • Each iteration binds the next element to the loop target.
  • The loop header is followed by an indented suite containing the repeated statements.
  • for is a compound control-flow statement.

Example: Iterate over an iterable

for item in items:
    print(item)

Prints each item from the iterable in iteration order.

When to use it

Collection traversal

Process every element in a collection.

Streaming iteration

Read each record from a generator or file.

Counted repetition

Perform an action for each value in a numeric range.

  • while
  • break
  • continue
  • else clause
  • iterable
  • range

Source: 8. Compound statements — Python 3.15.0 documentation. Checked 2026-10-15.