def describe(value):
match value:
caseint(n) if n > 0:
return f"positive int: {n}"case str(s) if len(s) > 5:
return f"long string: {s!r}"case [first, *rest]:
return f"list starting with {first}, {len(rest)} more"case {"type": "error", "code": code}:
return f"error code {code}"case _:
return"something else"print(describe(42))
print(describe([1, 2, 3]))
print(describe({"type": "error", "code": 500}))
output
positive int: 42
list starting with 1, 2 more
error code 500
Note Match/case supports literal, capture, sequence, mapping, class, guard (if), and OR (|) patterns. The _ wildcard matches anything.
match patternsguard clausepattern typesstructural matchingcase guard
f-string Improvements (3.12+)
syntax
f"{'quoted'} {expr!r}"
example
# Nested quotes and backslashes now allowed (3.12+)
users = [{"name": "Alice"}, {"name": "Bob"}]
print(f"First user: {users[0]['name']}")
# Multi-line f-string expressions
result = f"Total: {
sum(
item['price']
for item in [{'price': 10}, {'price': 20}]
)
}"print(result)
output
First user: Alice
Total: 30
Note Python 3.12 lifted the restriction on backslashes and same-quote nesting inside f-string expressions. This makes many previously awkward patterns simple.
# New syntax (3.12+) - no more TypeVar boilerplatedef first_element[T](items: list[T]) -> T:
return items[0]
print(first_element([10, 20, 30]))
print(first_element(["a", "b"]))
# Generic classclass Pair[A, B]:
def __init__(self, left: A, right: B):
self.left = left
self.right = right
p = Pair("name", 42)
print(p.left, p.right)
output
10
a
name 42
Note The [T] syntax on def/class replaces manual TypeVar declarations. It is scoped to the function or class, unlike module-level TypeVar which could be reused accidentally.
type parametergeneric functiongeneric classPEP 695TypeVar syntax
Note TaskGroup replaces asyncio.gather() with structured concurrency. If any task raises, all other tasks are cancelled and errors are collected into an ExceptionGroup.
# Python 3.13 introduced:# 1. Experimental JIT compiler (--enable-experimental-jit)# 2. Free-threaded mode (no GIL) via python3.13t# 3. Improved error messages with colorimport sys
print(f"Python {sys.version}")
# Check if GIL is disabled (3.13+)# sys._is_gil_enabled() # returns False in free-threaded build
Note Python 3.13's free-threaded build removes the GIL experimentally. Most C extensions need updating to work without the GIL. The JIT is opt-in and best for CPU-bound loops.
Python 3.13JITno GILfree-threadedperformance
warnings.deprecated (3.13+)
syntax
from warnings import deprecated
@deprecated("Use new_func instead")
# DeprecationWarning: process_v1: Use process_v2() instead
Note The built-in @deprecated decorator (3.13+, PEP 702) is recognized by type checkers, which will warn callers at analysis time, not just at runtime.