🆕 1. Walrus Operator (:=) – Python 3.8
Assign values inside expressions.
n = len(my_list)
if n > 5:
print(f"List is long: {n} elements")
# With Walrus Operator
if (n := len(my_list)) > 5:
print(f"List is long: {n} elements")
🆕 2. Positional-Only Parameters – Python 3.8
Restrict arguments to be positional only using /.
pythonCopyEditdef greet(name, /):
print(f"Hello, {name}!")
greet("Alice") # ✅
greet(name="Alice") # ❌ TypeError
🆕 3. f-string Debugging – Python 3.8
Print both expression and value with =.
pythonCopyEditx = 10
print(f"{x=}") # Output: x=10
🆕 4. Type Hinting Enhancements – Python 3.9+
Use built-in generics like list, tuple, dict for type hints.
pythonCopyEditdef greet_all(names: list[str]) -> None:
for name in names:
print(f"Hello {name}")
🆕 5. Pattern Matching (match-case) – Python 3.10
A more readable alternative to long if-elif chains.
def http_error(status):
match status:
case 400:
return "Bad Request"
case 404:
return "Not Found"
case 418:
return "I'm a teapot"
case _:
return "Something else"
🆕 6. Union Types with | Operator – Python 3.10
Cleaner syntax for optional or multiple types.
pythonCopyEditdef square(num: int | float) -> int | float:
return num * num
🆕 7. Exception Groups and except* – Python 3.11
Handle multiple exceptions raised in parallel (e.g., in async code).
try:
raise ExceptionGroup("group", [ValueError("a"), TypeError("b")])
except* ValueError as e:
print("Caught value error:", e)
🆕 8. Self Type Hint – Python 3.11
Used to annotate methods that return instances of their class.
from typing import Self
class MyBuilder:
def set_value(self, val) -> Self:
self.val = val
return self
🆕 9. Mutable vs Immutable Data Classes – Python 3.10+
from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
x: int
y: int
frozen=True makes it immutable like a namedtuple.
🆕 10. New Syntax Features in Python 3.12
- Faster, simpler f-string parsing
- More powerful error messages
- Subinterpreters for parallelism (experimental)
# 3.12 allows multi-line f-strings and comments inside them
name = "Alice"
age = 30
print(f"""
Hello, {name}
You are {age} years old # This is valid now in 3.12
""")
| hello | world | beautiful |
|---|---|---|
| nice | world | new |
| second | journey | column |