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_suiteParameters & Attributes
| Name | Type | Description |
|---|---|---|
| target | assignment target | Receives each item. |
| iterable | expression | Evaluated once to obtain an iterator. |
| suite | statement suite | Body executed for each item. |
| completion_suite | statement suite | Optional normal-completion body. |
Working Example
Example Snippetpython
seen=[]for key,value in [('a',1),('b',2)]: seen.append(key*value)print((seen,key))