Since Python 3.8, code can use the so-called "walrus" operator (:=), documented in PEP 572, for assignment expressions.
This seems like a really substantial new feature, since it allows this form of assignment within comprehensions and lambdas.
What exactly are the syntax, semantics, and grammar specifications of assignment expressions?
Why was this new (and seemingly quite radical) concept introduced, even though PEP 379 (which proposes the similar idea of "Adding an assignment expression") was withdrawn?
PEP 572 contains many of the details, especially for the first question. I'll try to summarise/quote concisely arguably some of the most important parts of the PEP:
Rationale
Allowing this form of assignment within comprehensions, such as list comprehensions, and lambda functions where traditional assignments are forbidden. This can also facilitate interactive debugging without the need for code refactoring.
Recommended use-case examples
a) Getting conditional values
for example (in Python 3):
can become:
Similarly, from the docs:
b) Simplifying list comprehensions
for example:
can become:
Syntax and semantics
Differences from regular assignment statements
In addition to being an expression rather than statement, there are several differences mentioned in the PEP: expression assignments go right-to-left, have different priority around commas, and do not support:
Multiple targets
Assignments not to a single name:
Iterable packing/unpacking
Inline type annotations:
Augmented assignment is not supported: