Two of the most significant recent additions to Python's syntax are the walrus operator (3.8) and the match statement (3.10), both aimed at making common patterns more concise and readable.
# Walrus operator (:=) — assign and use a value in the same expression
data = [1, 2, 3, 4, 5]
if (n := len(data)) > 3:
print(f"List has {n} items, which is more than 3") # n is usable here too
# match statement (structural pattern matching)
def describe(value):
match value:
case 0:
return "zero"
case int() | float() if value < 0: # guard clause + type pattern
return "negative number"
case [x, y]: # matches a 2-element list, unpacks it
return f"pair: {x}, {y}"
case {"name": name, **rest}: # matches a dict with a 'name' key
return f"named: {name}"
case _: # wildcard, like 'default'
return "something else"
print(describe([1, 2])) # "pair: 1, 2"
:= (nicknamed the "walrus operator" for its eye-like appearance) assigns a value to a name and produces that value as the expression's result, in one step — most useful in if/while conditions where you'd otherwise compute a value once to check it, then compute it again to use it.
match (Python 3.10+) is structural pattern matching, considerably more powerful than a simple switch from other languages: it can match against literal values, types, sequence/mapping shapes (destructuring them at the same time, as with case [x, y] above), and include if guard clauses for extra conditions — case _: is the wildcard, matching anything not caught by an earlier case. It's especially well-suited to parsing structured data (API responses, AST nodes, command arguments) where the shape of the data determines what to do with it.