Language Syntax

For — Python syntax

Obtain an iterator once and assign each item to the target. Else runs on normal exhaustion, not on break. The loop variable is not scoped to the loop. The else suite is optional.

Formal Syntax & Structure

python syntaxpython
for target in iterable:    suiteelse:    completion_suite

Parameters & Attributes

NameTypeDescription
targetassignment targetReceives each item.
iterableexpressionEvaluated once to obtain an iterator.
suitestatement suiteBody executed for each item.
completion_suitestatement suiteOptional normal-completion body.

Working Example

Example Snippetpython
seen=[]for key,value in [('a',1),('b',2)]:    seen.append(key*value)print((seen,key))
Python Reference