Prerequisites & Notation

Before You Begin

This chapter builds directly on the Python fundamentals from Chapter 1. You should be comfortable with basic Python syntax, data structures, and the data model before proceeding.

  • Python functions: def, return, arguments, default values(Review ch01)

    Self-check: Can you write a function that takes a list and an optional threshold and returns filtered elements?

  • Python data model: dunder methods, operator overloading(Review ch01)

    Self-check: Can you explain what __repr__ and __add__ do on a custom class?

  • Data structures: lists, dicts, sets, tuples(Review ch01)

    Self-check: Can you choose between a list and a dict for a lookup-heavy task?

  • Control flow: for loops, comprehensions, generators(Review ch01)

    Self-check: Can you rewrite a for-loop as a list comprehension or generator expression?

  • Basic NumPy: arrays, indexing, broadcasting

    Self-check: Can you create a NumPy array and perform element-wise operations?

Notation for This Chapter

Symbols and conventions introduced in this chapter. These build on the notation established in Chapter 1.

SymbolMeaningIntroduced
β€˜deff(x):...β€˜`def f(x): ...`Function definition β€” a callable object bound to name fs01
β€˜βˆ—argsβ€˜,β€˜βˆ—βˆ—kwargsβ€˜`*args`, `**kwargs`Variadic positional and keyword arguments (tuple and dict packing)s01
β€˜lambdax:exprβ€˜`lambda x: expr`Anonymous (unnamed) function β€” a single-expression callables01
β€˜closureβ€˜`closure`A function that captures variables from its enclosing scopes02
β€˜@decoratorβ€˜`@decorator`Decorator syntax β€” f = decorator(f) applied at definition times03
β€˜withctxasobj:β€˜`with ctx as obj:`Context manager protocol β€” calls __enter__ / __exit__ automaticallys04
β€˜functools.wrapsβ€˜`functools.wraps`Decorator that preserves the wrapped function's metadatas03
β€˜yieldβ€˜`yield`Suspends a function, turning it into a generator; used in context managers via contextlibs04