Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Operator :=

Binds the result of an expression to a name and also yields that result.

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

Syntax

(name := expression)

Binds the result of an expression to a name and also yields that result.

Use a named expression to keep an intermediate value, such as a match object, for the body of a conditional. The right-hand expression is evaluated once.

The target must be a simple name. Attribute and subscript targets are invalid here. Parentheses are required in some contexts, including a standalone expression statement.

The binding is still available to later statements in the same scope.

Syntax

(name := expression)

(name := expression) · SyntaxError

Worked example

(n := 4 + 1) → 5
n → 5
(a[0] := 1) → SyntaxError
obj = object(); (obj.x := 3) → SyntaxError

Examples

print((count := len("oak")))
print(count + 2)

Result

3
5

The binding is still available to later statements in the same scope.

Sources