Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword for

Evaluate the iterable expression once, obtain its iterator and assign each next value to the target.

Version: Python 3.14; repository image, production runtime not attested

Syntax

for target in iterable:
    ...

Evaluate the iterable expression once, obtain its iterator and assign each next value to the target.

Exhaustion ends the loop and permits its else suite. The loop does not clear its target; the body can reassign or delete it. An empty loop makes no assignment and preserves an existing binding.

Tuple targets unpack each item.

Examples

def pairs():
    print("source")
    return [(1, "a"), (2, "b")]
for key, value in pairs():
    print(key, value)
else:
    print("last", key)
for key in ():
    pass
print("after empty", key)
for item in [7]:
    del item
print("item" in globals())

Result

source
1 a
2 b
last 2
after empty 2
False

Tuple targets unpack each item.

Sources