After this lesson, you will be able to: Use modern Python 3.12+ syntax for variables, types, control flow, functions, and modules.
The basics done right. Python 3.12+ has match/case, structural typing, better error messages, use them.
Read every line; the comments explain idioms most tutorials skip.
# Types are inferred but you can annotatename: str = "Alex"age: int = 30active: bool = Truescores: list[int] = [85, 92, 78] # Python 3.9+, no need for List from typing# f-strings (always use these)print(f"{name} is {age}")print(f"{age=}") # 3.8+ debug form: outputs 'age=30'# Walrus operator (3.8+)if (n := len(scores)) > 0:print(f"{n} scores")# Match (3.10+), like switch but real pattern matchingdef classify(value):match value:case int() if value > 100: return "big int"case int(): return "int"case [first, *rest]: return f"list starting with {first}"case {"name": name}: return f"dict with name {name}"case _: return "other"# Functions with type hints + defaultsdef greet(name: str, greeting: str = "Hello") -> str:return f"{greeting}, {name}"# Keyword-only args (after *)def build_user(name: str, *, email: str, admin: bool = False) -> dict:return {"name": name, "email": email, "admin": admin}# Imports: prefer explicitfrom datetime import datetimeimport json # standard libraryfrom mypackage.users import get_user # your package
Never use `[]` or `{}` as a default. They are SHARED across calls. Use `None` and create inside.
Using mutable defaults. Comparing with `==` when you should use `is` (only for None/True/False/singletons). Catching bare `except:` which swallows KeyboardInterrupt. Writing `for i in range(len(xs)):` instead of `for i, x in enumerate(xs):`. Mixing tabs and spaces.
Pick the canonical Python gotcha.
Sign in and purchase access to unlock this lesson.