Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword lambda

Create a function object from one expression and return the expression’s value when called.

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

Syntax

lambda x: x + offset

Create a function object from one expression and return the expression’s value when called.

Free variables normally resolve at call time, so lambdas created in a loop can share the final binding. A default parameter evaluated at creation can preserve the current value.

Statements such as return cannot appear inside the expression body.

Examples

def build():
    late = []
    fixed = []
    for i in range(3):
        late.append(lambda: i)
        fixed.append(lambda i=i: i)
    return [f() for f in late], [f() for f in fixed]
print(build())

Result

([2, 2, 2], [0, 1, 2])

Statements such as return cannot appear inside the expression body.

Sources