Python Assignment Expressions
Learn how Python assignment expressions use := to bind a name inside a larger expression, when that improves clarity, and where ordinary assignment is the better choice.
Intermediate
Python normally binds a name with an assignment statement such as total = calculate(). An assignment expression lets you bind a name while also producing that value as part of a larger expression.
The := operator
The basic form is name := expression. Python evaluates the expression, binds its result to the name, and the assignment expression itself produces that same result. The feature was added in Python 3.8 and is part of current Python 3.14.
Output
Ready to run.
The parentheses here make the captured value easy to see: status receives the function result, and the whole assignment expression evaluates to that result so the comparison can use it immediately.
Capture a value in an if condition
A common use is when you need the result of a calculation both for the condition and inside the selected block. This avoids calling the same function twice or calculating the same value in a separate statement just to test it.
Output
Ready to run.
Capture a value in a while condition
Assignment expressions can also make a loop condition capture the value that controls the loop. The name is updated each time the condition is evaluated.
Output
Ready to run.
Here next(commands) is evaluated once per condition check. The resulting command is saved in command, then the comparison decides whether the loop body runs.
Use captured values in expressions and comprehensions
An assignment expression can appear where an ordinary expression is allowed. This can be useful when a subexpression would otherwise be repeated, including in a comprehension filter.
Output
Ready to run.
There is an important scope rule here: a name bound by an assignment expression in a comprehension is bound in the containing scope, unlike the comprehension iteration variable itself. That behavior is useful when you intentionally need the captured value afterward, but it can also make code harder to understand, so use it deliberately.
Parentheses and restrictions
Some contexts require parentheses around an assignment expression. For example, an assignment expression cannot stand unparenthesized at the top level of an expression statement, so x := 10 is invalid while (x := 10) is syntactically valid. The target is a name; assignment expressions do not assign directly to an attribute or subscription.
Output
Ready to run.
Prefer clarity over cleverness
Assignment expressions are most useful when the captured value naturally belongs to the condition or surrounding expression. When an ordinary assignment statement expresses the intent more clearly, prefer the statement. Do not introduce := merely to make code shorter.
Output
Ready to run.
What assignment expressions do not replace
They do not replace ordinary assignment statements, which can use multiple targets, attributes, subscriptions, annotations, and augmented-assignment forms.
They do not create a new scope; the target follows Python name-binding rules for the surrounding scope.
They are an expression feature, so their value can participate in a larger expression.
Use normal assignment when it communicates the operation more directly.
For a final check, take the Python Assignment Expressions quiz — then complete the coding exercise.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.